﻿//using NuGet.Packaging;
using MiniExcelLibs;
using Newtonsoft.Json;
using PanhuaWin.NCModel;
using PanhuaWin.WMS2NC888;
using PanhuaWin.工厂.仓库.下线;
using PanhuaWin.通用;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PanhuaWin.仓库.下线
{
    public partial class Form大标签打印 : UIPage
    {
        //BarTender.Application ba = new BarTender.Application();
        BarTender.Application ba;
        BarTender.Format bfmt;
        int 开始行;
        int 结束行;
        int 开始列;
        int 结束列;
        private string NCDeleteProductUrl = ConfigurationManager.AppSettings["NCDeleteProductUrl"].ToString();

        private UIRadioButton 当前来源选项 = new UIRadioButton();
        private bool 是否已打印;
        public Form大标签打印()
        {
            InitializeComponent();
        }

        private void Form打印标签_Load(object sender, EventArgs e)
        {
            ba = new BarTender.Application();
            bfmt = ba.Formats.Open(System.Windows.Forms.Application.StartupPath + "\\攀华大标签.btw");
            this.uiDatePicker开始日期.Value = DateTime.Today.AddDays(-1);
            this.uiDatePicker结束日期.Value = DateTime.Today.AddDays(-1);
            uiDataGridView产出.AutoGenerateColumns = false;
            pub.ListWorkShop(pub.sys_user, this.uiComboBoxWorkShop);
            ListMaterial();
        }


        private void uiButton查询_Click(object sender, EventArgs e)
        {
            listData();
        }

        private void listData()
        {
            if (uiRadioButton酸洗.Checked)
            {
                queryLablePrint("酸洗");
            }

            if (uiRadioButton轧硬.Checked)
            {
                queryLablePrint("轧硬");
            }
            if (uiRadioButton镀锌.Checked)
            {
                queryLablePrint("镀锌");
            }
            if (uiRadioButton彩涂.Checked)
            {
                queryLablePrint("彩涂");
            }
            if (uiRadioButton罩退.Checked)
            {
                queryLablePrint("罩退");
            }
            if (uiRadioButton退火.Checked)
            {
                queryLablePrint("退火");
            }
            if (uiRadioButton硅钢.Checked)
            {
                queryLablePrint("硅钢");
            }
            if (uiRadioButton期初.Checked)
            {
                queryLablePrint("期初打印");
            }
            if (uiRadioButton船运.Checked)
            {
                queryLablePrint("船运");
            } 
            //if (uiRadioButton镀铝锌线.Checked)
            //{
            //    queryLablePrint("镀铝锌线");
            //}
        }

        private void queryLablePrint(string 来源种类)
        {
            decimal 总重量;
            decimal 总长度;
            int 总卷数;
            总重量 = 0;
            总长度 = 0;
            string sql = @"select  guid, 来源种类, 来源产线, 来源班组, 工厂编号, 生产批号, 存货编号, 品名, 钢号,卷径, 条码, 厚度, 产地, 长度, 规格, 
                           等级, 表面状态, 表面处理, 净重, 备注, 生产日期, 检验员, 操作ip, 操作人员, 操作时间, 仓库, 货位, 锌层, 包装形式, 班组, 
                           客户, 颜色, 是否已打印, 打印次数, 是否抛送NC, 抛NC结果,品名EN
                            from Bus_LablePrint where 1=1";

            if (来源种类 == "镀锌")
            {
                sql += " and (来源种类 like '%镀锌%' or 来源种类 like '%彩基板%')  ";
            }
            else
            if (来源种类 == "罩退")
            {
                sql += "and (来源种类 like '%罩退%' OR 来源种类 like '%冷轧%')";
            }
            else
            {
                sql += " and 来源种类 like '%" + 来源种类 + "%' ";
            }

            if (checkBox包含已打印.Checked)
            {
                sql += " and (是否已打印 = '' or 是否已打印 is null or 是否已打印 = '是')";
            }
            else
            {
                sql += " and (是否已打印 = '' or 是否已打印 is null or 是否已打印 = '否')";
            }
            if (来源种类 == "船运")
            {
                sql += " and 操作时间 >= '" + this.uiDatePicker开始日期.Value + "' and 操作时间 < '" + this.uiDatePicker结束日期.Value.AddDays(1) + "'";

            }
            else
            {
                sql += " and 生产日期 >= '" + this.uiDatePicker开始日期.Value + "' and 生产日期 < '" + this.uiDatePicker结束日期.Value.AddDays(1) + "'";

            }

            if (!string.IsNullOrEmpty(uiTextBox规格型号1.Text.Trim()))
            {
                sql += " and 规格 = '" + uiTextBox规格型号1.Text.Trim() + "'";
            }
            if (!string.IsNullOrEmpty(uiComboBoxWorkShop.SelectedText))
            {
                sql += " and 工厂编号 = '" + pub.全局工厂编号 + "'";
            }
            if (!string.IsNullOrEmpty(uiTextBox品名.Text.Trim()))
            {
                sql += " and 品名 = '" + uiTextBox品名.Text.Trim() + "'";
            }

            if (string.IsNullOrEmpty(uiTextBox生产批号.Text.Trim()) == false)
            {
                sql += " and 生产批号 like '%" + uiTextBox生产批号.Text.Trim().ToUpper() + "%'";
            }
            if (!string.IsNullOrEmpty(uiComboBox材质.Text.Trim()) && uiComboBox材质.Text.Trim() != "请选择")
            {
                sql += " and 钢号 = '" + uiComboBox材质.Text.Trim() + "'";
            }

            if (!string.IsNullOrEmpty(uiComboBox产线.Text.Trim()) && uiComboBox产线.Text.Trim() != "请选择")
            {
                sql += " and 来源产线 = '" + uiComboBox产线.Text.Trim() + "'";
            }
            sql += " and 仓库 not in ('菲律宾加工件库','辅料库','南京库','上海库','五金库','江联扬中库','江联扬中万都库')";
            //要删除
            //sql += " and 来源种类 like '%测试%' ";
            ////sql += " and 生产批号 in (select 条码 from[Bus_AllCheckResult] where 1 = 1 and guid = 'A8426F4E-DCFB-2CF5-2977-EF87900229D2' and 条码 not in (select 卷 from.[张家港NC库存$])) ";
            ////sql += " and 仓库 in ('镀锌一线','镀锌二线','镀铝锌线','镀铝锌2库','保税加工','出口库','华达码头')";
            //sql += " order by 生产批号";
            ////sql += " group by  [来源种类], [来源产线], [来源班组], [工厂编号], [生产批号], [存货编号], [品名], [钢号], [条码], [厚度], [产地], [长度], [规格], [等级], [表面状态], [表面处理], [净重], [备注], [生产日期], 仓库, 货位, 锌层,包装形式,客户,是否已打印, 打印次数,Bus_LablePrint.颜色 order by Bus_LablePrint.品名,规格, 生产批号 ";
            DataTable dt = pub.GetDataTable(pub.ConnectionString, sql);
            uiDataGridView产出.AutoGenerateColumns = false;
            uiDataGridView产出.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            uiDataGridView产出.DataSource = dt;
            uiDataGridView产出.Refresh();

            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    //this.uiDataGridView产出.Rows[i].Cells["C选择"].Value = false;
                    decimal.TryParse(dt.Rows[i]["净重"]?.ToString(), out decimal a);
                    decimal 净重 = a;
                    总重量 += 净重;
                    decimal.TryParse(dt.Rows[i]["长度"].ToString(), out decimal b);
                    总长度 += b;
                }
                this.uiTextBox总卷数.Text = dt?.Rows.Count.ToString();
                this.uiTextBox总重量.Text = Math.Round(总重量, 0).ToString();
                this.uiTextBox总米数.Text = 总长度.ToString();
            }

           
        }

        private void uiRadioButton期初_CheckedChanged(object sender, EventArgs e)
        {
            当前来源选项 = sender as UIRadioButton;
        }
        private void uiRadioButton酸洗_CheckedChanged(object sender, EventArgs e)
        {
            当前来源选项 = sender as UIRadioButton;
            ListWorkLine((sender as UIRadioButton).Text, this.uiComboBox产线);
        }

        private void ListWorkLine(string WorkType, UIComboBox uiComboBox产线)
        {
            string sql;
            sql = @"SELECT '' WorkLineCode,'请选择' WorkLineName
                    union all
                    SELECT   WorkLineCode, WorkLineName
                    FROM      Dic_WorkLine where WorkType='" + WorkType + "' order by WorkLineCode";
            DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
            uiComboBox产线.DataSource = tb;
            uiComboBox产线.DisplayMember = "WorkLineName";
            uiComboBox产线.ValueMember = "WorkLineCode";
            uiComboBox产线.Refresh();
        }
        public void ListMaterial()
        {
            string sql;
            sql = @"  select '请选择' ProductMaterial
                    union all
                    select distinct ProductMaterial from Dic_WorkTypeMaterial";
            DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
            if (tb.Rows.Count > 0)
            {
                uiComboBox材质.DataSource = tb;
                uiComboBox材质.DisplayMember = "ProductMaterial";
                uiComboBox材质.ValueMember = "ProductMaterial";
                uiComboBox材质.Refresh();
            }
            else
            {
                uiComboBox材质.DataSource = null;

                uiComboBox材质.Refresh();
            }
        }
        private void uiRadioButton轧硬_CheckedChanged(object sender, EventArgs e)
        {
            当前来源选项 = sender as UIRadioButton;
            ListWorkLine((sender as UIRadioButton).Text, this.uiComboBox产线);
        }

        private void uiRadioButton镀锌_CheckedChanged(object sender, EventArgs e)
        {
            当前来源选项 = sender as UIRadioButton;
            ListWorkLine((sender as UIRadioButton).Text, this.uiComboBox产线);
        }

        private void uiRadioButton彩涂_CheckedChanged(object sender, EventArgs e)
        {
            当前来源选项 = sender as UIRadioButton;
            ListWorkLine((sender as UIRadioButton).Text, this.uiComboBox产线);
        }

        private void uiRadioButton罩退_CheckedChanged(object sender, EventArgs e)
        {
            当前来源选项 = sender as UIRadioButton;
            ListWorkLine((sender as UIRadioButton).Text, this.uiComboBox产线);
        }

        private void uiRadioButton退火_CheckedChanged(object sender, EventArgs e)
        {
            当前来源选项 = sender as UIRadioButton;
            ListWorkLine((sender as UIRadioButton).Text, this.uiComboBox产线);
        }

        private void uiRadioButton硅钢_CheckedChanged(object sender, EventArgs e)
        {
            当前来源选项 = sender as UIRadioButton;
            ListWorkLine((sender as UIRadioButton).Text, this.uiComboBox产线);
        }

        private void uiDataGridView产出_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1 && this.uiDataGridView产出.Columns[e.ColumnIndex].Name == "C打印大标签")
            {

                打印(e.RowIndex);
                //if (dt.Rows[e.RowIndex]["存货编号"] != null && !string.IsNullOrEmpty(dt.Rows[e.RowIndex]["存货编号"].ToString().Trim()))
                //{

                //    string 工厂编号 = dt.Rows[e.RowIndex]["工厂编号"].ToString();
                //    //string 生产批号 = dt.Rows[e.RowIndex]["生产批号"].ToString();
                //    //string 品名 = dt.Rows[e.RowIndex]["品名"].ToString();
                //    //string 钢号 = dt.Rows[e.RowIndex]["钢号"].ToString();
                //    string 厚度 = dt.Rows[e.RowIndex]["厚度"].ToString();
                //    string 长度 = dt.Rows[e.RowIndex]["长度"].ToString();
                //    //string 规格 = dt.Rows[e.RowIndex]["规格"].ToString();
                //    string 等级 = dt.Rows[e.RowIndex]["等级"].ToString();
                //    string 表面状态 = dt.Rows[e.RowIndex]["表面状态"].ToString();
                //    string 表面处理 = dt.Rows[e.RowIndex]["表面处理"].ToString();
                //    string 净重 = dt.Rows[e.RowIndex]["净重"].ToString();
                //    string 备注 = dt.Rows[e.RowIndex]["备注"].ToString();
                //    string 生产日期 = dt.Rows[e.RowIndex]["生产日期"].ToString();
                //    string departCode = queryDeptCode(当前来源选项.Tag.ToString(), 生产批号);
                //    string 仓库 = dt.Rows[e.RowIndex]["仓库"].ToString();
                //    string 锌层 = dt.Rows[e.RowIndex]["锌层"].ToString();
                //    string 包装形式 = dt.Rows[e.RowIndex]["包装形式"].ToString();
                //    //string 产地 = dt.Rows[e.RowIndex]["产地"].ToString();
                //    //产地暂时用万达
                //    string 产地 = "万达";
                //    //塞入 Bus_Volume
                //  int res =  insertIntoBus_Volume(工厂编号, 生产批号, 存货编号, departCode, 品名, 钢号, 长度, 厚度,  规格, 等级, 表面状态, 表面处理, 净重, 备注, 生产日期, 产地, 仓库, 锌层, 包装形式);
                //    //生成入库单 检查这个批号有没有生成过
                //    string sqlqueryIsInstock = @"select * from Bus_StockInV
                //                                left join Bus_StockInVolume on Bus_StockInV.guid = Bus_StockInVolume.guid
                //                                where VolumeCodeNew =  '" + 生产批号 + "'";
                //    DataTable dtIsInstock = pub.GetDataTable(pub.ConnectionString, sqlqueryIsInstock);
                //    if (dtIsInstock != null && dtIsInstock.Rows.Count > 0)
                //    {
                //        int r = print(来源种类, 工厂编号, 生产批号, 品名, 钢号, 长度, 规格, 等级, 表面状态, 表面处理, 净重, 备注, 生产日期);
                //        if (dtIsInstock.Rows[0]["IsSendToNC"].ToString() == "N")
                //        {
                //            pub.sendToNC产成品(dtIsInstock.Rows[0]["DocCode"].ToString());
                //        }
                //    }
                //    else
                //    {
                //        int r = print(来源种类, 工厂编号, 生产批号, 品名, 钢号, 长度, 规格, 等级, 表面状态, 表面处理, 净重, 备注, 生产日期);
                //        string docCode = createBus_StockInV(工厂编号, 生产批号, 存货编号, departCode, 品名, 钢号, 长度, 厚度, 规格, 等级, 表面状态, 表面处理, 净重, 备注, 生产日期, 产地, 锌层, 包装形式, Customer, 仓库);
                //        pub.sendToNC产成品(docCode);
                //    }
                //}
                //else
                //{
                //    Form补录存货编码 form补录存货编码 = new Form补录存货编码();
                //    form补录存货编码.shortName = 品名;
                //    form补录存货编码.INVSPEC = 规格;
                //    form补录存货编码.INVTYPE = 钢号;
                //    if (form补录存货编码.ShowDialog(this) == DialogResult.OK)
                //    {
                //        if (!string.IsNullOrEmpty(form补录存货编码.存货编号))
                //        {
                //            string 存货号 = form补录存货编码.存货编号;
                //            //更新编码
                //            string sqlupdateLabelPrintInvCode = "update Bus_LablePrint set 存货编号 = '"+存货号 + "' where 生产批号 = '"+ 生产批号 + "'";
                //            pub.ExecuteSql(pub.ConnectionString, sqlupdateLabelPrintInvCode, "更新Bus_LablePrint表 生产批号：【" + 生产批号 + "】的存货编号【" + 存货编号 + "】");
                //            update产出InvCode(来源种类,生产批号, 存货号);
                //            listData();
                //        }
                //        else
                //        {
                //            MessageBox.Show("缺少存货编号，不能打印");
                //        }

                //    }
                //    else
                //    {
                //        MessageBox.Show("缺少存货编号，不能打印");
                //    }

                //}
            }

            if (e.RowIndex > -1 && this.uiDataGridView产出.Columns[e.ColumnIndex].Name == "C删除")
            {
                string 卷号 = uiDataGridView产出.Rows[e.RowIndex].Cells["C生产批号"].Value.ToString();
                if (MessageBox.Show($"确定删除{卷号}么？", "", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    //检查有没有发送NC，发送了要提示
                    string sql = $"select 是否抛送NC from Bus_LablePrint where 生产批号 = '{卷号}'";
                    DataTable dt = pub.GetDataTable(pub.ConnectionString,sql);
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        if (dt.Rows[0][0].ToString().ToUpper() == "Y")
                        {
                            if (MessageBox.Show($"{卷号}已经推送到NC，请确定已经从NC删除了该卷，再从WMS删除！", "", MessageBoxButtons.OKCancel) == DialogResult.OK)
                            {
                               int r = deleteLabelPrint(卷号);
                                deleteBus_Volume(卷号);
                                deleteBus_StockInV(卷号);
                                if (r > 0)
                                {
                                    MessageBox.Show("删除成功");
                                }
                                else
                                {
                                    MessageBox.Show("删除失败");
                                }
                            }
                        }
                        else
                        {
                            //没发送过直接删除
                            int r = deleteLabelPrint(卷号);
                            deleteBus_Volume(卷号);
                            deleteBus_StockInV(卷号);
                            if (r > 0)
                            {
                                MessageBox.Show("删除成功");
                            }
                            else
                            {
                                MessageBox.Show("删除失败");
                            }
                        }
                    }

                    
                }
            }
            if (e.RowIndex > -1 && this.uiDataGridView产出.Columns[e.ColumnIndex].Name == "C还原为未打印")
            {
                string 生产批号 = this.uiDataGridView产出.Rows[e.RowIndex].Cells["C生产批号"].Value.ToString();
                if (MessageBox.Show("此操作会把【" + 生产批号 + "】更新为未打印未发送NC。请确定已经在NC里删除了这个卷！", "警告", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    string sql = "update Bus_LablePrint set 是否抛送NC = 'N' ,是否已打印 = '否' where 生产批号 = '" + 生产批号 + "'";
                    pub.ExecuteSql(pub.ConnectionString, sql, "更新为未打印未发送");
                    deleteBus_Volume(生产批号);
                    deleteBus_StockInV(生产批号);
                }
            }
        }

        private int deleteLabelPrint(string 卷号)
        {
            string sql = $"delete from Bus_LablePrint where 生产批号 = '{卷号}'";
            int r = pub.ExecuteSql(pub.ConnectionString, sql);
            return r;
        }


        private void update产出InvCode(string 来源种类, string 生产批号, string 存货编号)
        {
            if (来源种类.Contains("镀锌"))
            {
                string updateBus_zincoutput = "upate bus_zincoutput set InventoryCode = '" + 存货编号 + "' where OutputVolumeNew = '" + 生产批号 + "'";
                pub.ExecuteSql(pub.ConnectionString, updateBus_zincoutput, "更新镀锌产出表OutputVolumeNew：【" + 生产批号 + "】的存货编号【" + 存货编号 + "】");
            }
            if (来源种类.Contains("酸洗"))
            {
                string updateBus_zincoutput = "upate Bus_PicklingOutput set InventoryCode = '" + 存货编号 + "' where OutputVolumeNew = '" + 生产批号 + "'";
                pub.ExecuteSql(pub.ConnectionString, updateBus_zincoutput, "更新酸洗产出表OutputVolumeNew：【" + 生产批号 + "】的存货编号【" + 存货编号 + "】");
            }
            if (来源种类.Contains("轧硬"))
            {
                string updateBus_zincoutput = "upate Bus_HardOutput set InventoryCode = '" + 存货编号 + "' where OutputVolumeNew = '" + 生产批号 + "'";
                pub.ExecuteSql(pub.ConnectionString, updateBus_zincoutput, "更新轧硬产出表OutputVolumeNew：【" + 生产批号 + "】的存货编号【" + 存货编号 + "】");
            }
            if (来源种类.Contains("彩涂"))
            {
                string updateBus_zincoutput = "upate Bus_PaintOutput set InventoryCode = '" + 存货编号 + "' where OutputVolumeNew = '" + 生产批号 + "'";
                pub.ExecuteSql(pub.ConnectionString, updateBus_zincoutput, "更新彩涂产出表OutputVolumeNew：【" + 生产批号 + "】的存货编号【" + 存货编号 + "】");
            }
            if (来源种类.Contains("罩退"))
            {
                string updateBus_zincoutput = "upate Bus_CoverFireOutput set InventoryCode = '" + 存货编号 + "' where OutputVolumeNew = '" + 生产批号 + "'";
                pub.ExecuteSql(pub.ConnectionString, updateBus_zincoutput, "更新罩退产出表OutputVolumeNew：【" + 生产批号 + "】的存货编号【" + 存货编号 + "】");
            }
            if (来源种类.Contains("退火"))
            {
                string updateBus_zincoutput = "upate Bus_FireOutput set InventoryCode = '" + 存货编号 + "' where OutputVolumeNew = '" + 生产批号 + "'";
                pub.ExecuteSql(pub.ConnectionString, updateBus_zincoutput, "更新退火产出表OutputVolumeNew：【" + 生产批号 + "】的存货编号【" + 存货编号 + "】");
            }
            if (来源种类.Contains("硅钢"))
            {
                string updateBus_zincoutput = "upate Bus_SiOutput set InventoryCode = '" + 存货编号 + "' where OutputVolumeNew = '" + 生产批号 + "'";
                pub.ExecuteSql(pub.ConnectionString, updateBus_zincoutput, "更新硅钢产出表OutputVolumeNew：【" + 生产批号 + "】的存货编号【" + 存货编号 + "】");
            }
        }

        private int print(string 来源种类, string 工厂编号, string 生产批号, string 品名, string 钢号, string 长度, string 规格, string 等级, string 表面状态, string 表面处理, string 净重, string 标准, string 生产日期,string 卷径,string 品名EN)
        {
            if (checkBox打英文标签.Checked == true)
            {
               return printEN(来源种类, 工厂编号, 生产批号, 品名, 钢号, 长度, 规格, 等级, 表面状态, 表面处理,  净重, 标准, 生产日期, 卷径, 品名EN);
            }
            else
            {
                bfmt = ba.Formats.Open(System.Windows.Forms.Application.StartupPath + "\\打印模板\\攀华大标签.btw");
                bfmt.SetNamedSubStringValue("生产批号", "");
                bfmt.SetNamedSubStringValue("品名", "");
                bfmt.SetNamedSubStringValue("钢号", "");
                bfmt.SetNamedSubStringValue("长度", "");
                bfmt.SetNamedSubStringValue("规格", "");
                bfmt.SetNamedSubStringValue("等级", "");
                bfmt.SetNamedSubStringValue("表面状态", "");
                bfmt.SetNamedSubStringValue("表面处理", "");
                bfmt.SetNamedSubStringValue("净重", "");
                bfmt.SetNamedSubStringValue("备注", "");
                bfmt.SetNamedSubStringValue("生产日期", "");
                //string 生产批号 = dt.Rows[index]["生产批号"].ToString();
                //string 品名 = dt.Rows[index]["品名"].ToString();
                //string 钢号 = dt.Rows[index]["钢号"].ToString();
                //string 长度 = dt.Rows[index]["长度"].ToString();
                //string 规格 = dt.Rows[index]["规格"].ToString();
                //string 等级 = dt.Rows[index]["等级"].ToString();
                //string 表面状态 = dt.Rows[index]["表面状态"].ToString();
                //string 表面处理 = dt.Rows[index]["表面处理"].ToString();
                //string 净重 = dt.Rows[index]["净重"].ToString();
                //string 备注 = dt.Rows[index]["备注"].ToString();
                //string 生产日期 = dt.Rows[index]["生产日期"].ToString();

                //BarTender.Application ba = new BarTender.Application();
                //BarTender.Format bfmt;
                //bfmt = ba.Formats.Open(System.Windows.Forms.Application.StartupPath + "\\攀华大标签.btw");
                //bfmt.SetNamedSubStringValue("生产批号", dt.Rows[index]["生产批号"].ToString());
                //bfmt.SetNamedSubStringValue("品名", dt.Rows[index]["品名"].ToString());
                //bfmt.SetNamedSubStringValue("钢号", dt.Rows[index]["钢号"].ToString());
                //bfmt.SetNamedSubStringValue("长度", dt.Rows[index]["长度"].ToString());
                //bfmt.SetNamedSubStringValue("规格", dt.Rows[index]["规格"].ToString());
                //bfmt.SetNamedSubStringValue("等级", dt.Rows[index]["等级"].ToString());
                //bfmt.SetNamedSubStringValue("表面状态", dt.Rows[index]["表面状态"].ToString());
                //bfmt.SetNamedSubStringValue("表面处理", dt.Rows[index]["表面处理"].ToString());
                //bfmt.SetNamedSubStringValue("净重", dt.Rows[index]["净重"].ToString());
                //bfmt.SetNamedSubStringValue("备注", dt.Rows[index]["备注"].ToString());
                //bfmt.SetNamedSubStringValue("生产日期", dt.Rows[index]["生产日期"].ToString());
                //bfmt = ba.Formats.Open(System.Windows.Forms.Application.StartupPath + "\\攀华大标签.btw");
                if (表面状态.Trim() == "普通")
                {
                    表面状态 = "";
                }
                bfmt.SetNamedSubStringValue("生产批号", 生产批号);
                bfmt.SetNamedSubStringValue("品名", 品名);
                bfmt.SetNamedSubStringValue("钢号", 钢号);
                bfmt.SetNamedSubStringValue("卷径", 卷径);
                bfmt.SetNamedSubStringValue("长度", 长度);
                bfmt.SetNamedSubStringValue("规格", 规格);
                bfmt.SetNamedSubStringValue("等级", 等级);
                bfmt.SetNamedSubStringValue("表面状态", 表面状态);
                bfmt.SetNamedSubStringValue("表面处理", 表面处理);
                bfmt.SetNamedSubStringValue("净重", 净重);
                bfmt.SetNamedSubStringValue("备注", 标准);
                bfmt.SetNamedSubStringValue("生产日期", 生产日期);
                bfmt.PrintOut();

                //string sql = "insert into Bus_LablePrint(guid, 工厂编号, 生产批号,  品名, 钢号, 条码, 长度, 规格, 等级, 表面状态,  ";
                //sql += " 表面处理, 净重, 备注, 生产日期,  操作ip, 操作人员, 操作时间 ) ";
                //sql += " values(newid(),'" + 工厂编号 + "','" + 生产批号 + "',";
                //sql += " '" + 品名 + "','" + 钢号 + "','" + 生产批号 + "','" + 长度 + "','" + 规格 + "','" + 等级 + "','" + 表面状态 + "',";
                //sql += " '" + 表面处理 + "','" + 净重 + "','" + 标准 + "','" + 生产日期 + "','" + pub.getMac() + "','" + pub.sys_user.UserTrueName + "',getdate())";
                //pub.ExecuteSql(pub.ConnectionString, sql, "临时大标签打印塞入");
                //ba.Quit();

                string sql = "update  Bus_LablePrint set 是否已打印 = '是',打印次数 = ISNULL(打印次数,0)+1 where 生产批号 ='" + 生产批号 + "'";
                int r = pub.ExecuteSql(pub.ConnectionString, sql, "大标签打印次数更新");
                return r;
            }   
            
        }


        private int printEN(string 来源种类, string 工厂编号, string 生产批号, string 品名, string 钢号, string 长度, string 规格, string 等级, string 表面状态, string 表面处理, string 净重, string 标准, string 生产日期, string 卷径,string 品名EN)
        {

            bfmt = ba.Formats.Open(System.Windows.Forms.Application.StartupPath + "\\打印模板\\攀华大标签EN.btw");
            bfmt.SetNamedSubStringValue("生产批号", "");
            bfmt.SetNamedSubStringValue("品名", "");
            bfmt.SetNamedSubStringValue("钢号", "");
            bfmt.SetNamedSubStringValue("长度", "");
            bfmt.SetNamedSubStringValue("规格", "");
            bfmt.SetNamedSubStringValue("等级", "");
            bfmt.SetNamedSubStringValue("表面状态", "");
            bfmt.SetNamedSubStringValue("表面处理", "");
            bfmt.SetNamedSubStringValue("净重", "");
            bfmt.SetNamedSubStringValue("备注", "");
            bfmt.SetNamedSubStringValue("生产日期", "");
            //string 生产批号 = dt.Rows[index]["生产批号"].ToString();
            //string 品名 = dt.Rows[index]["品名"].ToString();
            //string 钢号 = dt.Rows[index]["钢号"].ToString();
            //string 长度 = dt.Rows[index]["长度"].ToString();
            //string 规格 = dt.Rows[index]["规格"].ToString();
            //string 等级 = dt.Rows[index]["等级"].ToString();
            //string 表面状态 = dt.Rows[index]["表面状态"].ToString();
            //string 表面处理 = dt.Rows[index]["表面处理"].ToString();
            //string 净重 = dt.Rows[index]["净重"].ToString();
            //string 备注 = dt.Rows[index]["备注"].ToString();
            //string 生产日期 = dt.Rows[index]["生产日期"].ToString();

            //BarTender.Application ba = new BarTender.Application();
            //BarTender.Format bfmt;
            //bfmt = ba.Formats.Open(System.Windows.Forms.Application.StartupPath + "\\攀华大标签.btw");
            //bfmt.SetNamedSubStringValue("生产批号", dt.Rows[index]["生产批号"].ToString());
            //bfmt.SetNamedSubStringValue("品名", dt.Rows[index]["品名"].ToString());
            //bfmt.SetNamedSubStringValue("钢号", dt.Rows[index]["钢号"].ToString());
            //bfmt.SetNamedSubStringValue("长度", dt.Rows[index]["长度"].ToString());
            //bfmt.SetNamedSubStringValue("规格", dt.Rows[index]["规格"].ToString());
            //bfmt.SetNamedSubStringValue("等级", dt.Rows[index]["等级"].ToString());
            //bfmt.SetNamedSubStringValue("表面状态", dt.Rows[index]["表面状态"].ToString());
            //bfmt.SetNamedSubStringValue("表面处理", dt.Rows[index]["表面处理"].ToString());
            //bfmt.SetNamedSubStringValue("净重", dt.Rows[index]["净重"].ToString());
            //bfmt.SetNamedSubStringValue("备注", dt.Rows[index]["备注"].ToString());
            //bfmt.SetNamedSubStringValue("生产日期", dt.Rows[index]["生产日期"].ToString());
            //bfmt = ba.Formats.Open(System.Windows.Forms.Application.StartupPath + "\\攀华大标签.btw");
            if (表面状态.Trim() == "普通")
            {
                表面状态 = "";
            }
            bfmt.SetNamedSubStringValue("生产批号", 生产批号);
            bfmt.SetNamedSubStringValue("品名", 品名EN);
            bfmt.SetNamedSubStringValue("钢号", 钢号);
            bfmt.SetNamedSubStringValue("卷径", 卷径);
            bfmt.SetNamedSubStringValue("长度", 长度);
            bfmt.SetNamedSubStringValue("规格", 规格);
            bfmt.SetNamedSubStringValue("等级", 等级);
            bfmt.SetNamedSubStringValue("表面状态", 表面状态);
            bfmt.SetNamedSubStringValue("表面处理", 表面处理);
            bfmt.SetNamedSubStringValue("净重", 净重);
            bfmt.SetNamedSubStringValue("备注", 标准);
            bfmt.SetNamedSubStringValue("生产日期", 生产日期);
            bfmt.PrintOut();

            //string sql = "insert into Bus_LablePrint(guid, 工厂编号, 生产批号,  品名, 钢号, 条码, 长度, 规格, 等级, 表面状态,  ";
            //sql += " 表面处理, 净重, 备注, 生产日期,  操作ip, 操作人员, 操作时间 ) ";
            //sql += " values(newid(),'" + 工厂编号 + "','" + 生产批号 + "',";
            //sql += " '" + 品名 + "','" + 钢号 + "','" + 生产批号 + "','" + 长度 + "','" + 规格 + "','" + 等级 + "','" + 表面状态 + "',";
            //sql += " '" + 表面处理 + "','" + 净重 + "','" + 标准 + "','" + 生产日期 + "','" + pub.getMac() + "','" + pub.sys_user.UserTrueName + "',getdate())";
            //pub.ExecuteSql(pub.ConnectionString, sql, "临时大标签打印塞入");
            //ba.Quit();

            string sql = "update  Bus_LablePrint set 是否已打印 = '是',打印次数 = ISNULL(打印次数,0)+1 where 生产批号 ='" + 生产批号 + "'";
            int r = pub.ExecuteSql(pub.ConnectionString, sql, "大标签打印次数更新");
            return r;
        }

        //private int insertIntoBus_Volume(string 工厂编号, string 生产批号, string 存货编号, string departCode, string 品名, string 钢号, string 长度, string 厚度, string 规格, string 等级, string 表面状态, string 表面处理, string 净重, string 备注, string 生产日期, string 产地, string 仓库, string 锌层, string 包装形式, string 来源产线)
        //{
        //    //先检查有没有
        //    string sqlquery = @"select VolumeCodeNew from Bus_Volume where VolumeCodeNew = '" + 生产批号 + "'";
        //    DataTable dt = pub.GetDataTable(pub.ConnectionString, sqlquery);
        //    if (dt != null && dt.Rows.Count > 0)
        //    {
        //        return 0;
        //    }
        //    else
        //    {
        //        string sql = @"insert into Bus_Volume ( guid,[WorkShopCode],[VolumeCodeNew],[FinishSize] ,[InventoryCode],[Material],[Address],[IsInStorage],StorageLocation,[WarehouseCode],[ZinCeng],PackageType)";
        //        sql += "values";
        //        sql += "( '" + Guid.NewGuid().ToString() + "','" + 工厂编号 + "','" + 生产批号 + "','" + 规格 + "' ,'" + 存货编号 + "','" + 钢号 + "','" + 产地 + "','在库','','" + 仓库 + "','" + 锌层 + "','" + 包装形式 + "')";
        //        return pub.ExecuteSql(pub.ConnectionString, sql, "保存成品入库状态单据");
        //    }
        //}

        //private string createBus_StockInV(string 工厂编号, string 生产批号, string 存货编号, string departCode, string 品名, string 钢号, string 长度, string 厚度, string 规格, string 等级, string 表面状态, string 表面处理, string 净重, string 备注, string 生产日期, string 产地, string 锌层, string 包装形式, string Customer, string 仓库, string 来源产线, string 颜色)
        //{
        //    string sqlqueryIsInstock = @"select VolumeCodeNew,Bus_StockInV.DocCode from Bus_StockInV
        //                                        left join Bus_StockInVolume on Bus_StockInV.guid = Bus_StockInVolume.guid
        //                                        where VolumeCodeNew =  '" + 生产批号 + "'";
        //    DataTable dtIsInstock = pub.GetDataTable(pub.ConnectionString, sqlqueryIsInstock);
        //    if (dtIsInstock != null && dtIsInstock.Rows.Count > 0)
        //    {
        //        return "已入库";
        //    }
        //    else
        //    {
        //        string guid = Guid.NewGuid().ToString();
        //        string Bguid = Guid.NewGuid().ToString();
        //        string docCode = pub.GetDocCode("产品入库", 工厂编号);
        //        decimal.TryParse(厚度, out decimal a);
        //        string sql;
        //        sql = " insert into Bus_StockInV(guid, StockInType, WorkShopCode, DocCode,  DepartCode, CreateID, Creator, CreateDate,IsSendToNC)";
        //        sql += " values('" + guid + "','产品入库 ','" + 工厂编号 + "','" + docCode + "', '" + departCode + "','" + pub.sys_user.User_Id + "','" + pub.sys_user.UserTrueName + "', getdate(),'N')";

        //        sql += @" insert into Bus_StockInVolume
        //                (guid,Bguid, DocCode, RowNumber, WorkShopCode, WarehouseCode, StorageLocation, VolumeCodeNew, InventoryCode, Size, 
        //                Thick,  Material,CID,Virtual,FaceStatus,FaceDone, Weight, Address,ProductDate,Grade,Meter,[ZinCeng],PackageType,Customer,WorkLine, PingMing,Color ) values ('" +
        //                    guid + "','" + Bguid + "', '" + docCode + "', '1', '" + 工厂编号 + "', '" + 仓库 + "', '', '" + 生产批号 + "', '" + 存货编号 + "', '" + 规格 + "', '"
        //                    + a + "',  '" + 钢号 + "','','C','" + 表面状态 + "',' " + 表面处理 + "','" + 净重 + "', '" + 产地 + "','" + 生产日期 + "','" + 等级 + "','" + 长度 + "',";
        //        sql += "'" + 锌层 + "','" + 包装形式 + "','" + Customer + "','" + 来源产线 + "','" + 品名 + "','" + 颜色 + "')";
        //        int r = pub.ExecuteSql(pub.ConnectionString, sql, "保存成品入库单据");
        //        string sqlqueryHeader = "select * from Bus_StockInV where guid='" + guid + "' ";
        //        DataTable tbr = pub.GetDataTable(pub.ConnectionString, sqlqueryHeader);
        //        if (tbr.Rows.Count > 0)
        //        {
        //            string sqlqueryBody = "select * from Bus_StockInVolume where guid='" + guid + "'";
        //            DataTable tbb = pub.GetDataTable(pub.ConnectionString, sqlqueryBody);
        //            if (tbb.Rows.Count > 0)
        //            {
        //                MessageBox.Show("保存成功单号【" + docCode + "】!", "提示", MessageBoxButtons.OK);

        //                return docCode;
        //            }
        //            else
        //            {
        //                sql = "delete  Bus_StockInV  where guid='" + guid + "'";
        //                pub.ExecuteSql(pub.ConnectionString, sql, "因为保存失败了，删除原料入库单");
        //                MessageBox.Show("保存失败,代码【err1】!", "提示", MessageBoxButtons.OK);
        //                return docCode;
        //            }
        //        }
        //        else
        //        {
        //            sql = "delete  Bus_StockInVolume  where guid='" + guid + "'";

        //            pub.ExecuteSql(pub.ConnectionString, sql, "因为保存失败了，删除原料入库单");
        //            MessageBox.Show("保存失败,代码【err2】!", "提示", MessageBoxButtons.OK);
        //            return docCode;
        //        }
        //    }
        //}

        //只要成功就删除原来的，生成新的 
        private int insertIntoBus_Volume(string 工厂编号, string 生产批号, string 存货编号, string departCode, string 品名, string 钢号, string 长度, string 厚度, string 规格, string 等级, string 表面状态, string 表面处理, string 净重, string 备注, string 生产日期, string 产地, string 仓库, string 锌层, string 包装形式, string 来源产线)
        {
            //先检查有没有
            string sqlquery = @"select VolumeCodeNew from Bus_Volume where VolumeCodeNew = '" + 生产批号 + "'";
            DataTable dt = pub.GetDataTable(pub.ConnectionString, sqlquery);
            if (dt != null && dt.Rows.Count > 0)
            {
                //删除
                string sqlDelete = "delete from Bus_Volume where VolumeCodeNew = '" + 生产批号 + "'";
                int r = pub.ExecuteSql(pub.ConnectionString, sqlDelete, "删除标签记录表重新生成");
                if (r < 1)
                {
                    MessageBox.Show("删除标签记录表重新生成失败");
                }
            }

            //string sql = @"insert into Bus_Volume ( guid,[WorkShopCode],[VolumeCodeNew],[FinishSize] ,[InventoryCode],[Material],[FaceStatus], [FaceDone],[Address],[IsInStorage],StorageLocation,[WarehouseCode],[ZinCeng],PackageType,ProductDate)";
            //sql += "values";
            //sql += "( '" + Guid.NewGuid().ToString() + "','" + 工厂编号 + "','" + 生产批号 + "','" + 规格 + "' ,'" + 存货编号 + "','" + 钢号 + "','"+表面状态+"','"+表面处理+"','" + 产地 + "','在库','','" + 仓库 + "','" + 锌层 + "','" + 包装形式 + "','"+生产日期+"')";
            string sql = @"insert into Bus_Volume( [guid], [WorkShopCode],[VolumeCodeNew], [FinishSize], [InventoryCode], [InventoryName], [Material], [FaceStatus], [FaceDone], [FinishWeight], [Meter], [FinishRank], [ProductDate], [StorageLocation], [IsInStorage], [Customer], [Address], [WarehouseCode], [ZinCeng], [PackageType],Color)
                        select newid(),		[工厂编号]		,[生产批号],		[规格],    [存货编号],       [品名],			[钢号],   [表面状态], [表面处理]	, [净重], [长度], [等级], [生产日期],		[货位],					'在库'		, [客户], [产地], [仓库], [锌层],[包装形式],[颜色]
                        from Bus_LablePrint
                        where 生产批号 = '" + 生产批号 + "'";
            return pub.ExecuteSql(pub.ConnectionString, sql, "保存成品入库状态单据");

        }
        private int deleteBus_Volume(string 生产批号)
        {
            int r = 0;
            //先检查有没有
            //string sqlquery = @"select VolumeCodeNew from Bus_Volume where VolumeCodeNew = '" + 生产批号 + "'";
            //DataTable dt = pub.GetDataTable(pub.ConnectionString, sqlquery);
            //if (dt != null && dt.Rows.Count > 0)
            //{
            //删除
            string sqlDelete = "delete from Bus_Volume where VolumeCodeNew = '" + 生产批号 + "'";
            r = pub.ExecuteSql(pub.ConnectionString, sqlDelete, "删除标签记录表重新生成");
            //if (r < 1)
            //{
            //    MessageBox.Show("删除标签记录表重新生成失败");
            //}
            return r;
            //}
            //else
            //{
            //    return 1;
            //}

        }

        private string deleteBus_StockInV(string 工厂编号, string 生产批号, string 存货编号, string departCode, string 品名, string 钢号, string 长度, string 厚度, string 规格, string 等级, string 表面状态, string 表面处理, string 净重, string 备注, string 生产日期, string 产地, string 锌层, string 包装形式, string Customer, string 仓库, string 来源产线, string 颜色)
        {
            //查出 入库单
            string sqlqueryIsInstock = @"select VolumeCodeNew,Bus_StockInV.DocCode,Bus_StockInV.guid
                                        from Bus_StockInV
                                        left join Bus_StockInVolume on Bus_StockInV.guid = Bus_StockInVolume.guid
                                                where VolumeCodeNew =  '" + 生产批号 + "'";
            DataTable dtIsInstock = pub.GetDataTable(pub.ConnectionString, sqlqueryIsInstock);
            if (dtIsInstock != null && dtIsInstock.Rows.Count > 0)
            {
                string oldguid = dtIsInstock.Rows[0]["guid"].ToString();
                //删除
                string sqlDeleteStockV = "delete from Bus_StockInV where guid = '" + oldguid + "'";
                int deleStockV = pub.ExecuteSql(pub.ConnectionString,sqlDeleteStockV,"删除入库单重新生成");
                if (deleStockV < 1)
                {
                    MessageBox.Show("删除入库单头重新生成失败");
                }
                string sqlDeleteStockVolume = "delete from Bus_StockInVolume where guid = '"+ oldguid + "'";
                int deleStockVolume = pub.ExecuteSql(pub.ConnectionString, sqlDeleteStockVolume, "删除入库单重新生成");
                if (deleStockVolume < 1)
                {
                    MessageBox.Show("删除入库单体重新生成失败");
                }
            }

                string guid = Guid.NewGuid().ToString();
                string Bguid = Guid.NewGuid().ToString();
                string docCode = pub.GetDocCode("产品入库", 工厂编号);
                 docCode += "$" + guid;
                decimal.TryParse(厚度, out decimal a);
                string sql;
                sql = " insert into Bus_StockInV(guid, StockInType, WorkShopCode, DocCode,  DepartCode, CreateID, Creator, CreateDate,IsSendToNC)";
                sql += " values('" + guid + "','产品入库 ','" + 工厂编号 + "','" + docCode + "', '" + departCode + "','" + pub.sys_user.User_Id + "','" + pub.sys_user.UserTrueName + "', getdate(),'Y');";

                sql += @" insert into Bus_StockInVolume
                        (guid,Bguid, DocCode, RowNumber, WorkShopCode, WarehouseCode, StorageLocation, VolumeCodeNew, InventoryCode, Size, 
                        Thick,  Material,CID,Virtual,FaceStatus,FaceDone, Weight, Address,ProductDate,Grade,Meter,[ZinCeng],PackageType,Customer,WorkLine, PingMing,Color ) values ('" +
                            guid + "','" + Bguid + "', '" + docCode + "', '1', '" + 工厂编号 + "', '" + 仓库 + "', '', '" + 生产批号 + "', '" + 存货编号 + "', '" + 规格 + "', '"
                            + a + "',  '" + 钢号 + "','','C','" + 表面状态 + "',' " + 表面处理 + "','" + 净重 + "', '" + 产地 + "','" + 生产日期 + "','" + 等级 + "','" + 长度 + "',";
                sql += "'" + 锌层 + "','" + 包装形式 + "','" + Customer + "','" + 来源产线 + "','" + 品名 + "','" + 颜色 + "');";
            int r = pub.ExecuteSql(pub.ConnectionString, sql, "保存成品入库单据") ;
                string sqlqueryHeader = "select * from Bus_StockInV where guid='" + guid + "' ";
                DataTable tbr = pub.GetDataTable(pub.ConnectionString, sqlqueryHeader);
                if (tbr.Rows.Count > 0)
                {
                    string sqlqueryBody = "select * from Bus_StockInVolume where guid='" + guid + "'";
                    DataTable tbb = pub.GetDataTable(pub.ConnectionString, sqlqueryBody);
                    if (tbb.Rows.Count > 0)
                    {
                        //MessageBox.Show("保存成功单号【" + docCode + "】!", "提示", MessageBoxButtons.OK);

                        return docCode;
                    }
                    else
                    {
                        sql = "delete  Bus_StockInV  where guid='" + guid + "'";
                        pub.ExecuteSql(pub.ConnectionString, sql, "因为保存失败了，删除原料入库单");
                        MessageBox.Show("保存失败,代码【err1】!", "提示", MessageBoxButtons.OK);
                        return docCode;
                    }
                }
                else
                {
                    sql = "delete  Bus_StockInVolume  where guid='" + guid + "'";

                    pub.ExecuteSql(pub.ConnectionString, sql, "因为保存失败了，删除原料入库单");
                    MessageBox.Show("保存失败,代码【err2】!", "提示", MessageBoxButtons.OK);
                    return docCode;
                }
        }
        private int deleteBus_StockInV(string 生产批号)
        {
            int r = 0;
            //查出 入库单
            string sqlqueryIsInstock = @"select VolumeCodeNew,Bus_StockInV.DocCode,Bus_StockInV.guid
                                        from Bus_StockInV
                                        left join Bus_StockInVolume on Bus_StockInV.guid = Bus_StockInVolume.guid
                                                where VolumeCodeNew =  '" + 生产批号 + "'";
            DataTable dtIsInstock = pub.GetDataTable(pub.ConnectionString, sqlqueryIsInstock);
            if (dtIsInstock != null && dtIsInstock.Rows.Count > 0)
            {
                string oldguid = dtIsInstock.Rows[0]["guid"].ToString();
                //删除
                string sqlDeleteStockV = "delete from Bus_StockInV where guid = '" + oldguid + "'";
                r = pub.ExecuteSql(pub.ConnectionString, sqlDeleteStockV, "删除入库单重新生成");

                string sqlDeleteStockVolume = "delete from Bus_StockInVolume where guid = '" + oldguid + "'";
                r = pub.ExecuteSql(pub.ConnectionString, sqlDeleteStockVolume, "删除入库单重新生成");
            }
            return r;

        }

        private NCResponseModel sendToNC产成品(string 工厂编号, string 仓库编号, string 产线, string guid, string 存货编码, string 批次号, string 重量, string 生产日期, string 品名
                                   , string 颜色, string 长度, string 产地, string 锌层, string 等级, string 包装, string 客户, string 表面处理,string 货位)
        {

            //生产日期
            DateTime.TryParse(生产日期, out DateTime date);

            NCModel.FinishedProductsInBillModel setInboundModel = new NCModel.FinishedProductsInBillModel();
            //setInboundModel.corpName = "";
            setInboundModel.region = "华中";
            setInboundModel.ceateDate = DateTime.Parse(pub.GetTime()).ToString("yyyy-MM-dd");
            //查询公司名称 公司编号
            string sqlworkshop = @"SELECT  [WorkShopName]  FROM [Dic_WorkShop] where WorkShopCode = '" + 工厂编号 + "'";
            DataTable dtWorkShop = pub.GetDataTable(pub.ConnectionString, sqlworkshop);
            if (dtWorkShop != null && dtWorkShop.Rows.Count > 0)
            {
                setInboundModel.corpName = dtWorkShop.Rows[0]["WorkShopName"].ToString();
                //setInboundModel.corpName = "张家港万达薄板有限公司";
            }
            else
            {
                MessageBox.Show("未能根据当前入库单公司编号查到对应的公司名称");
                return null;
            }
            //查仓库名称
            string sqlwareName = "select WarehouseName from Dic_Warehouse where WarehouseCode = '" + 仓库编号 + "'";

            DataTable dtWareName = pub.GetDataTable(pub.ConnectionString, sqlwareName);
            if (dtWareName != null && dtWareName.Rows.Count > 0)
            {
                setInboundModel.cWarehouseName = dtWareName.Rows[0]["WarehouseName"].ToString();
            }
            else
            {
                setInboundModel.cWarehouseName = 仓库编号;
            }
            //setInboundModel.cWarehouseName = "涪镀锌一线成品库";
            setInboundModel.cWhsmanagerCode = "";
            setInboundModel.deptName = 产线;
            setInboundModel.cbizCode = "";
            setInboundModel.remarks = "";

            List<NCModel.Item> itemList = new List<NCModel.Item>();

            NCModel.Item item = new NCModel.Item();
            //明细ID
            item.rowNo = guid;
            //存货编码
            item.cinvbascode = 存货编码;
            //批次号
            item.vbatchcode = 批次号;
            //入库数量 填重量
            decimal.TryParse(重量, out decimal a);
            decimal 重量2 = Math.Round(a / 1000, 4);
            item.ninnum = 重量2.ToString();
            //入库日期  生产日期
            item.dbilldate = date.ToString("yyyy-MM-dd");
            //颜色 只有彩涂卷才有颜色
            if (品名 != null && !string.IsNullOrEmpty(品名))
            {
                if (品名.Contains("彩涂"))
                {
                    item.vfree1 = 颜色;
                }
                else
                {
                    item.vfree1 = "";
                }
            }

            //长度
            item.vfree2 = 长度;
            //产地
            string sql = $"select 卷号 from View_发岳阳已发货 where 卷号 = '{批次号}'";
            DataTable dt = pub.GetDataTable(pub.ConnectionString, sql);
            if (dt != null && dt.Rows.Count > 0)
            {
                item.vfree3 = 产地;
            }
            else
            {
                if (string.IsNullOrWhiteSpace(产地))
                {
                    item.vfree3 = "攀达";
                }
                else
                {
                    item.vfree3 = 产地;
                }
                if (string.IsNullOrWhiteSpace(品名) == false && 品名.Contains("无锌"))
                {
                    item.vfree3 = "攀达零锌花";
                }
            }
            //表面状态  
            if (string.IsNullOrEmpty(表面处理))
            {
                item.vfree4 = "常规";
            }
            else
            {
                item.vfree4 = 表面处理;
            }
            

            //表面厚度（锌层
            if (string.IsNullOrEmpty(锌层))
            {
                item.vfree5 = "普通";
            }
            else
            {
                item.vfree5 = 锌层;
            }
            
            //等级 
            item.vuserdef8 = 等级;
            //包装
            item.vuserdef10 = 包装;
            //客户
            item.pk_defdoc13 = 客户;
            item.nprice = "";
            item.nmny = "";
            item.pk_defdoc11 = "";
            item.pk_defdoc12 = "";
            item.vnotebody = "";
            //货位
            item.vuserdef9 = 货位;

            item.scrq = date.ToString("yyyy-MM-dd");
            itemList.Add(item);

            setInboundModel.items = itemList;
            string jsonStr = JsonConvert.SerializeObject(setInboundModel);
            ExternalSystemToNcItfPortTypeClient client = new ExternalSystemToNcItfPortTypeClient();
            string responseStr = client.finishedProductsInBill(jsonStr);
            NCModel.NCResponseModel nCResponse = JsonConvert.DeserializeObject<NCModel.NCResponseModel>(responseStr);
            return nCResponse;
            //调用成功，改写NC状态

        }

        private NCResponseModel sendToNC产成品(string 工厂编号, string 仓库编号, string 产线, string guid, string 存货编码, string 批次号, string 重量, string 生产日期, string 品名
                                   , string 颜色, string 长度, string 产地, string 锌层, string 等级, string 包装, string 客户, string 表面处理, string 货位,string 备注)
        {
            //生产日期
            DateTime.TryParse(生产日期, out DateTime date);

            NCModel.FinishedProductsInBillModel setInboundModel = new NCModel.FinishedProductsInBillModel();
            //setInboundModel.corpName = "";
            setInboundModel.region = "华中";
            setInboundModel.ceateDate = DateTime.Parse(pub.GetTime()).ToString("yyyy-MM-dd");
            //查询公司名称 公司编号
            string sqlworkshop = @"SELECT  [WorkShopName]  FROM [Dic_WorkShop] where WorkShopCode = '" + 工厂编号 + "'";
            DataTable dtWorkShop = pub.GetDataTable(pub.ConnectionString, sqlworkshop);
            if (dtWorkShop != null && dtWorkShop.Rows.Count > 0)
            {
                setInboundModel.corpName = dtWorkShop.Rows[0]["WorkShopName"].ToString();
                //setInboundModel.corpName = "张家港万达薄板有限公司";
            }
            else
            {
                MessageBox.Show("未能根据当前入库单公司编号查到对应的公司名称");
                return null;
            }
            //查仓库名称
            string sqlwareName = "select WarehouseName from Dic_Warehouse where WarehouseCode = '" + 仓库编号 + "'";

            DataTable dtWareName = pub.GetDataTable(pub.ConnectionString, sqlwareName);
            if (dtWareName != null && dtWareName.Rows.Count > 0)
            {
                setInboundModel.cWarehouseName = dtWareName.Rows[0]["WarehouseName"].ToString();
            }
            else
            {
                setInboundModel.cWarehouseName = 仓库编号;
            }
            //setInboundModel.cWarehouseName = "涪镀锌一线成品库";
            setInboundModel.cWhsmanagerCode = "";
            setInboundModel.deptName = 产线;
            setInboundModel.cbizCode = "";
            setInboundModel.remarks = 备注;

            List<NCModel.Item> itemList = new List<NCModel.Item>();

            NCModel.Item item = new NCModel.Item();
            //明细ID
            item.rowNo = guid;
            //存货编码
            item.cinvbascode = 存货编码;
            //批次号
            item.vbatchcode = 批次号;
            //入库数量 填重量
            decimal.TryParse(重量, out decimal a);
            decimal 重量2 = Math.Round(a / 1000, 4);
            item.ninnum = 重量2.ToString();
            //入库日期  生产日期
            item.dbilldate = date.ToString("yyyy-MM-dd");
            //颜色 只有彩涂卷才有颜色
            if (品名 != null && !string.IsNullOrEmpty(品名))
            {
                if (品名.Contains("彩涂"))
                {
                    item.vfree1 = 颜色;
                }
                else
                {
                    item.vfree1 = "";
                }
            }

            //长度
            item.vfree2 = 长度;
            //产地
            if (string.IsNullOrEmpty(产地))
            {
                item.vfree3 = "攀达";
            }
            else
            {
                item.vfree3 = 产地;
            }

            //表面状态  
            if (string.IsNullOrEmpty(表面处理))
            {
                item.vfree4 = "常规";
            }
            else
            {
                item.vfree4 = 表面处理;
            }


            //表面厚度（锌层
            if (string.IsNullOrEmpty(锌层))
            {
                item.vfree5 = "普通";
            }
            else
            {
                item.vfree5 = 锌层;
            }

            //等级 
            item.vuserdef8 = 等级;
            //包装
            item.vuserdef10 = 包装;
            //客户
            item.pk_defdoc13 = 客户;
            item.nprice = "";
            item.nmny = "";
            item.pk_defdoc11 = "";
            item.pk_defdoc12 = "";
            item.vnotebody = "";
            //货位
            item.vuserdef9 = 货位;

            item.scrq = date.ToString("yyyy-MM-dd");
            itemList.Add(item);

            setInboundModel.items = itemList;
            string jsonStr = JsonConvert.SerializeObject(setInboundModel);
            ExternalSystemToNcItfPortTypeClient client = new ExternalSystemToNcItfPortTypeClient();
            string responseStr = client.finishedProductsInBill(jsonStr);
            NCModel.NCResponseModel nCResponse = JsonConvert.DeserializeObject<NCModel.NCResponseModel>(responseStr);
            return nCResponse;
            //调用成功，改写NC状态

        }

        private string queryDeptCode(string tableName, string OutputVolumeNew)
        {
            DataTable dt = new DataTable();
            //string sql = @"select [OutputGuid], [guid], [Row], [VolumeCodeNew], [OutputVolumeNew], [OutputSize], [OutputThick], [OutputWidth], [OutputMaterial], [OutputWeight],
            //                [OutputMeter], [OutputQualityGrade], [OutputQualityInformation], [OutputEndStatus], [OutputNext], [OutputAddress], [PrintTimes], [DepartCode], [DepartName],
            //                [WorkLineCode], [WorkLineName], [WorkTeamCode], [WorkTeamName], [DayShiftCode], [DayShiftName], [CreateID], [Creator], [CreateDate], [ModifyID], [Modifier],
            //                [ModifyDate] where OutputVolumeNew = '" + OutputVolumeNew + "'";
            string sql = @"select   [DepartCode], [DepartName]
                            from " + tableName + " where OutputVolumeNew = '" + OutputVolumeNew + "'";
            dt = pub.GetDataTable(pub.ConnectionString, sql);
            if (dt != null && dt.Rows.Count > 0)
            {
                return dt.Rows[0]["DepartCode"].ToString();
            }
            else
            {
                return "";

            }

        }

        private void uiButton匹配NC存货编号_Click(object sender, EventArgs e)
        {

            // DataTable dt = uiDataGridView产出.DataSource as DataTable;
            if (uiDataGridView产出 != null && uiDataGridView产出.Rows != null && uiDataGridView产出.Rows.Count > 0)
            {
                for (int i = 0; i < uiDataGridView产出.Rows.Count; i++)
                {
                    if (this.uiDataGridView产出.Rows[i].Cells["C选择"].Value != null)//打钩
                    {
                        if ((bool)this.uiDataGridView产出.Rows[i].Cells["C选择"].Value)
                        {
                            string 存货编号 = uiDataGridView产出.Rows[i].Cells["C存货编号"].Value.ToString();
                            string 生产批号 = uiDataGridView产出.Rows[i].Cells["C生产批号"].Value.ToString();
                            string 来源种类 = uiDataGridView产出.Rows[i].Cells["C来源种类"].Value.ToString();
                            string Customer = uiDataGridView产出.Rows[i].Cells["C客户"].Value.ToString();
                            string 品名 = uiDataGridView产出.Rows[i].Cells["C品名"].Value.ToString();
                            string 钢号 = uiDataGridView产出.Rows[i].Cells["C钢号"].Value.ToString();
                            string 规格 = uiDataGridView产出.Rows[i].Cells["C规格"].Value.ToString();
                            string 是否推送NC = uiDataGridView产出.Rows[i].Cells["C抛NC成功"].Value.ToString();
                            //有存货编号了也不能推
                            //if (!string.IsNullOrEmpty(存货编号))
                            //{
                            //    //MessageBox.Show("批号【" + 生产批号 + "】已经有存货编号了，无法修改存货编号");
                            //    continue;

                            //}
                            //判断是否推送NC
                            if (是否推送NC == "Y")
                            {
                                MessageBox.Show("批号【" + 生产批号 + "】已经推送到NC，无法修改存货编号");
                                continue;
                            }
                            //判断是否入库了
                            string sqlIsInStock = "select VolumeCodeNew from Bus_StockInVolume  ";
                            sqlIsInStock += " where  VolumeCodeNew ='" + 生产批号 + "' ";
                            DataTable tb = pub.GetDataTable(pub.ConnectionString, sqlIsInStock);
                            if (tb.Rows.Count > 0 )
                            {
                                MessageBox.Show("批号【" + 生产批号 + "】已经生成入库单，无法修改存货编号");
                                continue;
                            }

                            DataTable invCoeDt = GetInventoryCode(品名, 规格, 钢号);
                            if (invCoeDt != null && invCoeDt.Rows.Count > 1)
                            {
                                //弹出超过两个提示
                                MessageBox.Show("生产批号【"+ 生产批号 + "】根据品名【"+ 品名 + "】规格【"+ 规格 + "】钢号【"+ 钢号 + "】查询，发现存在重复的存货编号，请到【存货编码查询】界面复制正确的存货编号进行手动修改。");
                               
                                //显示弹窗让选择
                                //Form补录存货编码 form补录存货编码 = new Form补录存货编码();
                                //form补录存货编码.shortName = 品名;
                                //form补录存货编码.INVSPEC = 规格;
                                //form补录存货编码.INVTYPE = 钢号;
                                //if (form补录存货编码.ShowDialog(this) == DialogResult.OK)
                                //{
                                //    string invCode = form补录存货编码.存货编号;
                                //    updateLabelPrintInv(i, 生产批号, invCode);
                                //}
                            }
                            else
                            if (invCoeDt != null && invCoeDt.Rows.Count == 1)
                            {
                                string invCode = invCoeDt.Rows[0]["INVCODE"].ToString();
                                string FORINVNAME = invCoeDt.Rows[0]["FORINVNAME"].ToString();
                                if (!string.IsNullOrEmpty(invCode))
                                {
                                    //如果有存货编号了，则比较是否一致，如果一致就不更新。如果不一致就提示要不要更新。如果是空就直接更新
                                    updateLabelPrintInv(i, 生产批号, invCode, FORINVNAME);

                                }
                            }
                            else
                            {
                                MessageBox.Show("生产批号【" + 生产批号 + "】根据品名【" + 品名 + "】规格【" + 规格 + "】钢号【" + 钢号 + "】查询，没有对应的存货编号，请到NC确定是否有对应存货编号。");
                            }
                        }
                    }
                }

            }
        }
        //更新存货编号
        private void updateLabelPrintInv(int i, string 生产批号, string invCode)
        {
            string 存货编号 = uiDataGridView产出.Rows[i].Cells["C存货编号"].Value.ToString();
            if (string.IsNullOrEmpty(存货编号))
            {
                string sqlUpdate = " update Bus_LablePrint set 存货编号 = '" + invCode + "' where 生产批号 = '" + 生产批号 + "' and (存货编号 ='' or 存货编号 is null )";
                int r = pub.ExecuteSql(pub.ConnectionString, sqlUpdate);
                if (r <= 0)
                {
                    MessageBox.Show("更新失败");
                }
                else
                {
                    uiDataGridView产出.Rows[i].Cells["C存货编号"].Value = invCode;
                }
            }
            else if (invCode != 存货编号)
            {
                if (MessageBox.Show("确定要将批号【" + 生产批号 + "】的存货编号从【" + 存货编号 + "】改为【" + invCode + "】吗？", "警告", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    //将原来的存货编号更行掉
                    string sqlUpate = "update Bus_LablePrint set 存货编号 = '" + invCode + "' where 生产批号 = '" + 生产批号 + "'";
                    int r = pub.ExecuteSql(pub.ConnectionString, sqlUpate);
                    if (r <= 0)
                    {
                        MessageBox.Show("更新失败");
                    }
                    else
                    {
                        uiDataGridView产出.Rows[i].Cells["C存货编号"].Value = invCode;
                    }
                }
            }
        }

        private void updateLabelPrintInv(int i, string 生产批号, string invCode,string 品名EN)
        {
            string 存货编号 = uiDataGridView产出.Rows[i].Cells["C存货编号"].Value.ToString();
            if (string.IsNullOrEmpty(存货编号))
            {
                string sqlUpdate = " update Bus_LablePrint set 存货编号 = '" + invCode + "',品名EN = '"+ 品名EN + "' where 生产批号 = '" + 生产批号 + "' and (存货编号 ='' or 存货编号 is null )";
                int r = pub.ExecuteSql(pub.ConnectionString, sqlUpdate);
                if (r <= 0)
                {
                    MessageBox.Show("更新失败");
                }
                else
                {
                    uiDataGridView产出.Rows[i].Cells["C存货编号"].Value = invCode;
                    uiDataGridView产出.Rows[i].Cells["C品名EN"].Value = 品名EN;
                }
            }
            else if (invCode != 存货编号)
            {
                if (MessageBox.Show("确定要将批号【" + 生产批号 + "】的存货编号从【" + 存货编号 + "】改为【" + invCode + "】吗？", "警告", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    //将原来的存货编号更行掉
                    string sqlUpate = "update Bus_LablePrint set 存货编号 = '" + invCode + "',品名EN = '"+ 品名EN + "'  where 生产批号 = '" + 生产批号 + "'";
                    int r = pub.ExecuteSql(pub.ConnectionString, sqlUpate);
                    if (r <= 0)
                    {
                        MessageBox.Show("更新失败");
                    }
                    else
                    {
                        uiDataGridView产出.Rows[i].Cells["C存货编号"].Value = invCode;
                    }
                }
            }
        }

        private DataTable GetInventoryCode(string invshortname, string Size, string Material)
        {
            string sql;
            sql = "SELECT   guid, INVCODE, INVNAME, INVSHORTNAME, INVSPEC, INVTYPE, INVCLASSCODE, INVCLASSNAME, CreateID, Creator, ";
            sql += " CreateDate, ModifyID, Modifier, ModifyDate,FORINVNAME FROM      Dic_Inventory where INVSHORTNAME='" + invshortname + "' ";
            sql += " and INVSPEC='" + Size + "' and INVTYPE='" + Material + "'";
            DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
            return tb;
        }

        private void uiDataGridView产出_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            //if (e.RowIndex > -1 && uiDataGridView产出.Columns[e.ColumnIndex].Name == "C客户")
            //{
            //    DataTable dt = uiDataGridView产出.DataSource as DataTable;
            //    string 客户 = dt.Rows[e.RowIndex]["客户"].ToString();
            //    string 生产批号 = dt.Rows[e.RowIndex]["生产批号"].ToString();
            //    string sqlupdate = "update Bus_LablePrint set 客户 = '" + 客户 + "' where 生产批号 = '" + 生产批号 + "'";
            //    int r = pub.ExecuteSql(pub.ConnectionString, sqlupdate, "");
            //}
            if (e.RowIndex > -1 && uiDataGridView产出.Columns[e.ColumnIndex].Name == "C存货编号")
            {
                DataTable dt = uiDataGridView产出.DataSource as DataTable;
                string 存货编号 = dt.Rows[e.RowIndex]["存货编号"].ToString();
                string 生产批号 = dt.Rows[e.RowIndex]["生产批号"].ToString();
                string sqlupdate = "update Bus_LablePrint set 存货编号 = '" + 存货编号 + "' where 生产批号 = '" + 生产批号 + "'";
                int r = pub.ExecuteSql(pub.ConnectionString, sqlupdate, "");
            }
            //if (e.RowIndex > -1 && uiDataGridView产出.Columns[e.ColumnIndex].Name == "C钢号")
            //{
            //    DataTable dt = uiDataGridView产出.DataSource as DataTable;
            //    string 钢号 = dt.Rows[e.RowIndex]["客户"].ToString();
            //    string 生产批号 = dt.Rows[e.RowIndex]["生产批号"].ToString();
            //    string sqlupdate = "update Bus_LablePrint set 钢号 = '" + 钢号 + "' where 生产批号 = '" + 生产批号 + "'";
            //    int r = pub.ExecuteSql(pub.ConnectionString, sqlupdate, "");
            //}
        }

        private void checkBox是否已打印_CheckedChanged(object sender, EventArgs e)
        {
            listData();
        }

        private void Form打印标签_FormClosed(object sender, FormClosedEventArgs e)
        {
            ba.Quit();
        }

        private void uiButton批量打印_Click(object sender, EventArgs e)
        {
            string logsql = @"insert into Sys_Log( BeginDate, BrowserType, RequestParameter,UserName, User_Id ,UserIP,ServiceIP   )";
            logsql += "values(getdate(),'WinForm','调用NCservice','" + pub.sys_user.UserName + "','" + pub.sys_user.User_Id + "','" + MAC.getIP.getLocalIP() + "','WMS2NC888')";
            pub.ExecuteSql(pub.ConnectionString, logsql);

            uiButton批量打印.Enabled = false;
            try
            {
                //遍历行
                if (uiDataGridView产出 != null && uiDataGridView产出.Rows.Count > 0)
                {
                    for (int i = 0; i < uiDataGridView产出.Rows.Count; i++)
                    {
                        if (this.uiDataGridView产出.Rows[i].Cells["C选择"].Value != null)//打钩
                        {
                            if ((bool)this.uiDataGridView产出.Rows[i].Cells["C选择"].Value)
                            {
                                //检查有没有存货编号
                                string 存货编号 = uiDataGridView产出.Rows[i].Cells["C存货编号"].Value.ToString();
                                if (string.IsNullOrEmpty(存货编号))
                                {
                                    MessageBox.Show("【" + uiDataGridView产出.Rows[i].Cells["C生产批号"].Value.ToString() + "】的存货编号为空，无法推送");
                                    continue;
                                }

                                ////是否已经打印了
                                if (this.uiDataGridView产出.Rows[i].Cells["C是否已打印"].Value.ToString() == "是")
                                {
                                    if (MessageBox.Show("批号【" + uiDataGridView产出.Rows[i].Cells["C生产批号"].Value.ToString() + "】已经打印【" + uiDataGridView产出.Rows[i].Cells["C打印次数"].Value.ToString() + "】次，是否重新打印？"
                                        , "提示"
                                        , MessageBoxButtons.OKCancel) == DialogResult.OK)
                                    {
                                        打印(i);
                                    }
                                }
                                else
                                {
                                    // 没有打印过，直接打印
                                    打印(i);
                                }
                            }

                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace);
            }
            finally
            {
                uiButton批量打印.Enabled = true;
            }
        }


        private async void 打印(int i)
        {
            string guid = uiDataGridView产出.Rows[i].Cells["Cguid"].Value.ToString();
            string 存货编号 = uiDataGridView产出.Rows[i].Cells["C存货编号"].Value.ToString();
            string 生产批号 = uiDataGridView产出.Rows[i].Cells["C生产批号"].Value.ToString();
            string 来源种类 = uiDataGridView产出.Rows[i].Cells["C来源种类"].Value.ToString();
            string Customer = uiDataGridView产出.Rows[i].Cells["C客户"].Value.ToString();
            string 品名 = uiDataGridView产出.Rows[i].Cells["C品名"].Value.ToString();
            string 品名EN = uiDataGridView产出.Rows[i].Cells["C品名EN"].Value.ToString();
            string 钢号 = uiDataGridView产出.Rows[i].Cells["C钢号"].Value.ToString();
            string 卷径 = uiDataGridView产出.Rows[i].Cells["C卷径"].Value.ToString();
            string 规格 = uiDataGridView产出.Rows[i].Cells["C规格"].Value.ToString();
            string 工厂编号 = uiDataGridView产出.Rows[i].Cells["C工厂编号"].Value.ToString();
            string 来源产线 = uiDataGridView产出.Rows[i].Cells["C来源产线"].Value.ToString();

            string 厚度 = uiDataGridView产出.Rows[i].Cells["C厚度"].Value.ToString();
            string 长度 = uiDataGridView产出.Rows[i].Cells["C长度"].Value.ToString();
            string 颜色 = uiDataGridView产出.Rows[i].Cells["C颜色"].Value.ToString();
            //string 规格 = dt.Rows[e.RowIndex]["规格"].ToString();
            string 等级 = uiDataGridView产出.Rows[i].Cells["C等级"].Value.ToString();
            string 表面状态 = uiDataGridView产出.Rows[i].Cells["C表面状态"].Value.ToString();
            string 表面处理 = uiDataGridView产出.Rows[i].Cells["C表面处理"].Value.ToString();
            string 净重 = uiDataGridView产出.Rows[i].Cells["C净重"].Value.ToString();
            string 备注 = uiDataGridView产出.Rows[i].Cells["C备注"].Value.ToString();
            string 生产日期 = uiDataGridView产出.Rows[i].Cells["C生产日期"].Value.ToString();
            string departCode = queryDeptCode(当前来源选项?.Tag?.ToString(), 生产批号);
            string 仓库 = uiDataGridView产出.Rows[i].Cells["C仓库"].Value.ToString();
            string 货位 = uiDataGridView产出.Rows[i].Cells["C货位"].Value.ToString();
            string 锌层 = 表面状态;
            string 包装形式 = uiDataGridView产出.Rows[i].Cells["C包装形式"].Value.ToString();
            //来源是彩涂 但是品名是镀锌，按镀锌标准
            string sql标准 = "";
            if (当前来源选项.Text.ToString() == "彩涂" && 品名.Contains("镀锌"))
            {
                 sql标准 = "select QualityStandard from [dbo].[Dic_QualityStandard] where  WorkTypeName = '镀锌'";
            }
            else
            {
                 sql标准 = "select QualityStandard from [dbo].[Dic_QualityStandard] where  WorkTypeName = '" + 当前来源选项.Text.ToString() + "'";
            }

            DataTable dt标准 = pub.GetDataTable(pub.ConnectionString, sql标准);

            string 标准 = "";

            if (dt标准 != null && dt标准.Rows.Count > 0)
            {
                标准 = dt标准?.Rows[0][0].ToString();
            }
            else
            {
                标准 = 备注;
            }
           
            //产地暂时用万达
            string 产地 = uiDataGridView产出.Rows[i].Cells["C产地"].Value.ToString();
            string 抛NC成功 = uiDataGridView产出.Rows[i].Cells["C抛NC成功"].Value.ToString();

            //没有抛过
            if (抛NC成功 != "Y")
            {
                NCResponseModel nCResponse = sendToNC产成品(工厂编号, 仓库, 来源产线, guid, 存货编号, 生产批号, 净重, 生产日期, 品名,
                                                  颜色, 长度, 产地, 锌层, 等级, 包装形式, Customer, 表面处理, 货位);

                if (nCResponse != null && nCResponse.state == 0)
                {
                    string sqlUpdate = @"update Bus_LablePrint set 抛NC结果 = '" + nCResponse.result + "',是否抛送NC = 'Y' , 抛NC时间 = '" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "' where 生产批号 = '" + 生产批号 + "'";
                    int r = pub.ExecuteSql(pub.ConnectionString, sqlUpdate, "成品卷入库发送到NC");
                    //塞入入库单 stockVolume

                    if (r < 1)
                    {
                        MessageBox.Show("1更新是否成功发送到NC失败");
                    }
                    else
                    {
                        uiDataGridView产出.Rows[i].Cells["C抛NC成功"].Value = "Y";
                        uiDataGridView产出.Rows[i].Cells["C抛NC结果"].Value = nCResponse.result;
                        this.uiDataGridView产出.Rows[i].Cells["C选择"].Value = false;
                        if (品名.Contains("彩涂"))
                        {
                            //彩涂的表面处理就是颜色

                            print(来源种类, 工厂编号, 生产批号, 品名, 钢号, 长度, 规格, 等级, 表面状态, 颜色, 净重, 标准, 生产日期, 卷径, 品名EN);
                            //塞入 Bus_Volume
                            int res = insertIntoBus_Volume(工厂编号, 生产批号, 存货编号, departCode, 品名, 钢号, 长度, 厚度, 规格, 等级, 表面状态, 表面处理, 净重, 备注, 生产日期, 产地, 仓库, 锌层, 包装形式, 来源产线);
                            //生成入库单 检查这个批号有没有生成过
                            string docCode = deleteBus_StockInV(工厂编号, 生产批号, 存货编号, departCode, 品名, 钢号, 长度, 厚度, 规格, 等级, 表面状态, 表面处理, 净重, 备注, 生产日期, 产地, 锌层, 包装形式, Customer, 仓库, 来源产线, 颜色);
                        }
                        else
                        {
                            print(来源种类, 工厂编号, 生产批号, 品名, 钢号, 长度, 规格, 等级, 表面状态, 表面处理, 净重, 标准, 生产日期, 卷径, 品名EN);
                            //塞入 Bus_Volume
                            int res = insertIntoBus_Volume(工厂编号, 生产批号, 存货编号, departCode, 品名, 钢号, 长度, 厚度, 规格, 等级, 表面状态, 表面处理, 净重, 备注, 生产日期, 产地, 仓库, 锌层, 包装形式, 来源产线);

                            string docCode = deleteBus_StockInV(工厂编号, 生产批号, 存货编号, departCode, 品名, 钢号, 长度, 厚度, 规格, 等级, 表面状态, 表面处理, 净重, 备注, 生产日期, 产地, 锌层, 包装形式, Customer, 仓库, 来源产线, 颜色);
                        }
                    }
                }
                else
                {
                    if (品名.Contains("彩涂"))
                    {
                        //彩涂的表面处理就是颜色
                        print(来源种类, 工厂编号, 生产批号, 品名, 钢号, 长度, 规格, 等级, 表面状态, 颜色, 净重, 标准, 生产日期, 卷径, 品名EN);
                    }
                    else
                    {
                        print(来源种类, 工厂编号, 生产批号, 品名, 钢号, 长度, 规格, 等级, 表面状态, 表面处理, 净重, 标准, 生产日期, 卷径, 品名EN);

                    }


                    string sqlUpdate = @"update Bus_LablePrint set 抛NC结果 = '" + nCResponse?.msg + "',是否抛送NC = 'N' , 抛NC时间 = '" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "' where 生产批号 = '" + 生产批号 + "'";
                    int r = pub.ExecuteSql(pub.ConnectionString, sqlUpdate, "成品卷入库发送到NC");
                    if (r < 1)
                    {
                        MessageBox.Show("2更新是否成功发送到NC失败");
                    }
                    else
                    {
                        uiDataGridView产出.Rows[i].Cells["C抛NC成功"].Value = "N";
                        uiDataGridView产出.Rows[i].Cells["C抛NC结果"].Value = nCResponse.result;
                        this.uiDataGridView产出.Rows[i].Cells["C选择"].Value = false;
                    }

                }
            }
            else
            {
                //已经抛过，直接打印
                if (品名.Contains("彩涂"))
                {
                    //彩涂的表面处理就是颜色
                    print(来源种类, 工厂编号, 生产批号, 品名, 钢号, 长度, 规格, 等级, 表面状态, 颜色, 净重, 标准, 生产日期, 卷径, 品名EN);
                }
                else
                {
                    print(来源种类, 工厂编号, 生产批号, 品名, 钢号, 长度, 规格, 等级, 表面状态, 表面处理, 净重, 标准, 生产日期, 卷径, 品名EN);
                }
            }

        }
        private void createStockIn()
        {

        }
        private void uiDataGridView产出_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            开始行 = e.RowIndex;
            //this.uiLabel开始.Text = 开始行.ToString();

            开始列 = e.ColumnIndex;
        }

        private void uiDataGridView产出_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
        {
            decimal 总重量 = 0;
            decimal 总米数 = 0;
            decimal 卷数 = 0;
            结束行 = e.RowIndex;
            结束列 = e.ColumnIndex;
            //this.uiLabel结束.Text = 结束行.ToString();
            if (开始行 == 0 && 结束行 == 0)
            {
                //选择了就取消 没选择就选择
                if (this.uiDataGridView产出.Rows[开始行].Cells["C选择"].Value != null && (bool)this.uiDataGridView产出.Rows[开始行].Cells["C选择"].Value == true)//打钩
                {
                    this.uiDataGridView产出.Rows[开始行].Cells["C选择"].Value = false;
                }
                else
                if (this.uiDataGridView产出.Rows[开始行].Cells["C选择"].Value == null || (bool)this.uiDataGridView产出.Rows[开始行].Cells["C选择"].Value == false)
                {
                    this.uiDataGridView产出.Rows[开始行].Cells["C选择"].Value = true;

                }
            }

            if (开始行 >= 0 && 结束行 > 0)
            {
                if (开始行 > 结束行)
                {
                    int k = 开始行;
                    开始行 = 结束行;
                    结束行 = k;
                }
                for (int i = 开始行; i <= 结束行; i++)
                {
                    //选择了就取消 没选择就选择
                    if (this.uiDataGridView产出.Rows[i].Cells["C选择"].Value != null && (bool)this.uiDataGridView产出.Rows[i].Cells["C选择"].Value == true)//打钩
                    {
                        this.uiDataGridView产出.Rows[i].Cells["C选择"].Value = false;
                    }
                    else
                    if (this.uiDataGridView产出.Rows[i].Cells["C选择"].Value == null || (bool)this.uiDataGridView产出.Rows[i].Cells["C选择"].Value == false)
                    {
                        this.uiDataGridView产出.Rows[i].Cells["C选择"].Value = true;

                    }

                    卷数++;
                    总重量 += pub.Todecimal(this.uiDataGridView产出.Rows[i].Cells["C净重"].Value.ToString());
                    总米数 += pub.Todecimal(this.uiDataGridView产出.Rows[i].Cells["C长度"].Value.ToString());
                }

                this.uiTextBox总重量.Text = 总重量.ToString();
                this.uiTextBox总米数.Text = 总米数.ToString();
                this.uiTextBox总卷数.Text = 卷数.ToString();
            }
        }

        private void uiButton修改_Click(object sender, EventArgs e)
        {
            string 生产批号;
            string sql;
            string 结果值;

            结果值 = this.uiTextBox修改.Text.Trim().Replace("--", "");

            if (开始列 == 结束列)
            {
                if (开始行 >= 0)
                {
                    if (结束行 >= 0)
                    {
                        if (开始行 > 结束行)
                        {
                            int k = 开始行;
                            开始行 = 结束行;
                            结束行 = k;
                        }


                        if (this.uiDataGridView产出.Columns[开始列].Name == "C品名")
                        {

                            if (结果值 == "")
                            {

                            }
                            else
                            {

                                if (MessageBox.Show("您要批改所选择的品名么?更改品名后需要重新获取存货编号！", "", MessageBoxButtons.OKCancel) == DialogResult.OK)
                                {
                                    for (int i = 开始行; i <= 结束行; i++)
                                    {
                                        生产批号 = this.uiDataGridView产出.Rows[i].Cells["C生产批号"].Value.ToString();
                                        sql = "select VolumeCodeNew from Bus_StockInVolume  ";
                                        sql += " where  VolumeCodeNew ='" + 生产批号 + "' ";
                                        DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
                                        //if (tb.Rows.Count > 0)
                                        //{

                                        //}
                                        //else
                                        //{
                                            sql = "update  [Bus_LablePrint] set 品名='" + 结果值 + "' , 存货编号 = ''  where 生产批号='" + 生产批号 + "' ";
                                            int r = pub.ExecuteSql(pub.ConnectionString, sql, "打印大标签批量修改品名");
                                            if (r > 0)
                                            {
                                                this.uiDataGridView产出.Rows[i].Cells["C品名"].Value = 结果值;
                                            }
                                            else
                                            {
                                                MessageBox.Show("保存失败");
                                            }

                                        //}


                                    }
                                }
                            }

                        }
                        if (this.uiDataGridView产出.Columns[开始列].Name == "C钢号" )
                        {
                            if (结果值 == "")
                            {

                            }
                            else
                            {    //检查未塞入用友
                                if (MessageBox.Show("您要批改所选择的钢号值么?", "", MessageBoxButtons.OKCancel) == DialogResult.OK)
                                {
                                    for (int i = 开始行; i <= 结束行; i++)
                                    {
                                        sql = "update  [Bus_LablePrint] set 钢号='" + 结果值 + "'  where 生产批号='" + this.uiDataGridView产出.Rows[i].Cells["C生产批号"].Value.ToString() + "' ";
                                        int r = pub.ExecuteSql(pub.ConnectionString, sql, "");
                                        if (r > 0)
                                        {
                                            this.uiDataGridView产出.Rows[i].Cells["C钢号"].Value = 结果值;
                                        }
                                        else
                                        {
                                            MessageBox.Show("保存失败");
                                        }

                                    }
                                }
                            }

                        }

                        if (this.uiDataGridView产出.Columns[开始列].Name == "C卷径")
                        {
                            if (结果值 == "")
                            {

                            }
                            else
                            {    //检查未塞入用友
                                if (MessageBox.Show("您要批改所选择的卷径值么?", "", MessageBoxButtons.OKCancel) == DialogResult.OK)
                                {
                                    for (int i = 开始行; i <= 结束行; i++)
                                    {
                                        sql = "update  [Bus_LablePrint] set 卷径='" + 结果值 + "'  where 生产批号='" + this.uiDataGridView产出.Rows[i].Cells["C生产批号"].Value.ToString() + "' ";
                                        int r = pub.ExecuteSql(pub.ConnectionString, sql, "");
                                        if (r > 0)
                                        {
                                            this.uiDataGridView产出.Rows[i].Cells["C卷径"].Value = 结果值;
                                        }
                                        else
                                        {
                                            MessageBox.Show("保存失败");
                                        }

                                    }
                                }
                            }

                        }
                        if (this.uiDataGridView产出.Columns[开始列].Name == "C规格")
                        {
                            if (结果值 == "")
                            {

                            }
                            else
                            {    //检查未塞入用友
                                if (MessageBox.Show("您要批改所选择的规格值么?", "", MessageBoxButtons.OKCancel) == DialogResult.OK)
                                {
                                    for (int i = 开始行; i <= 结束行; i++)
                                    {
                                        sql = "update  [Bus_LablePrint] set 规格='" + 结果值 + "'  where 生产批号='" + this.uiDataGridView产出.Rows[i].Cells["C生产批号"].Value.ToString() + "' ";
                                        int r = pub.ExecuteSql(pub.ConnectionString, sql, "");
                                        if (r > 0)
                                        {
                                            this.uiDataGridView产出.Rows[i].Cells["C规格"].Value = 结果值;
                                        }
                                        else
                                        {
                                            MessageBox.Show("保存失败");
                                        }

                                    }
                                }
                            }
                        }
                        if (this.uiDataGridView产出.Columns[开始列].Name == "C净重" )
                        {
                            if (结果值 == "")
                            {

                            }
                            else
                            {    //检查未塞入用友
                                if (MessageBox.Show("您要批改所选择的净重值么?", "", MessageBoxButtons.OKCancel) == DialogResult.OK)
                                {
                                    for (int i = 开始行; i <= 结束行; i++)
                                    {
                                        sql = "update  [Bus_LablePrint] set 净重='" + 结果值 + "'  where 生产批号='" + this.uiDataGridView产出.Rows[i].Cells["C生产批号"].Value.ToString() + "' ";
                                        int r = pub.ExecuteSql(pub.ConnectionString, sql, "");

                                        if (r > 0)
                                        {
                                            this.uiDataGridView产出.Rows[i].Cells["C净重"].Value = 结果值;
                                        }
                                        else
                                        {
                                            MessageBox.Show("保存失败");
                                        }
                                    }
                                }
                            }
                        }
                        if (this.uiDataGridView产出.Columns[开始列].Name == "C长度" )
                        {
                            if (结果值 == "")
                            {

                            }
                            else
                            {    //检查未塞入用友
                                if (MessageBox.Show("您要批改所选择的长度值么?", "", MessageBoxButtons.OKCancel) == DialogResult.OK)
                                {
                                    for (int i = 开始行; i <= 结束行; i++)
                                    {
                                        sql = "update  [Bus_LablePrint] set 长度='" + 结果值 + "'  where 生产批号='" + this.uiDataGridView产出.Rows[i].Cells["C生产批号"].Value.ToString() + "' ";
                                        int r = pub.ExecuteSql(pub.ConnectionString, sql, "");
                                        if (r > 0)
                                        {
                                            this.uiDataGridView产出.Rows[i].Cells["C长度"].Value = 结果值;
                                        }
                                        else
                                        {
                                            MessageBox.Show("保存失败");
                                        }

                                    }
                                }
                            }
                        }

                        if (this.uiDataGridView产出.Columns[开始列].Name == "C生产日期" )
                        {
                            if (结果值 == "")
                            {

                            }
                            else
                            {    //检查未塞入用友
                                if (MessageBox.Show("您要批改所选择的生产日期值么?", "", MessageBoxButtons.OKCancel) == DialogResult.OK)
                                {
                                    for (int i = 开始行; i <= 结束行; i++)
                                    {
                                        sql = "update  [Bus_LablePrint] set 生产日期='" + 结果值 + "'  where 生产批号='" + this.uiDataGridView产出.Rows[i].Cells["C生产批号"].Value.ToString() + "' ";
                                        int r = pub.ExecuteSql(pub.ConnectionString, sql, "");
                                        if (r > 0)
                                        {
                                            this.uiDataGridView产出.Rows[i].Cells["C生产日期"].Value = 结果值;
                                        }
                                        else
                                        {
                                            MessageBox.Show("保存失败");
                                        }

                                    }
                                }
                            }
                        }

                        if (this.uiDataGridView产出.Columns[开始列].Name == "C来源产线" )
                        {
                            if (结果值 == "")
                            {

                            }
                            else
                            {    //检查未塞入用友
                                if (MessageBox.Show("您要批改所选择的来源产线值么?", "", MessageBoxButtons.OKCancel) == DialogResult.OK)
                                {
                                    for (int i = 开始行; i <= 结束行; i++)
                                    {
                                        sql = "update  [Bus_LablePrint] set 来源产线='" + 结果值 + "'  where 生产批号='" + this.uiDataGridView产出.Rows[i].Cells["C生产批号"].Value.ToString() + "' ";
                                        int r = pub.ExecuteSql(pub.ConnectionString, sql, "");
                                        if (r > 0)
                                        {
                                            this.uiDataGridView产出.Rows[i].Cells["C来源产线"].Value = 结果值;
                                        }
                                        else
                                        {
                                            MessageBox.Show("保存失败");
                                        }

                                    }
                                }
                            }
                        }

                        if (this.uiDataGridView产出.Columns[开始列].Name == "C仓库" )
                        {
                            if (结果值 == "")
                            {

                            }
                            else
                            {    //检查未塞入用友
                                if (MessageBox.Show("您要批改所选择的仓库值么?", "", MessageBoxButtons.OKCancel) == DialogResult.OK)
                                {
                                    for (int i = 开始行; i <= 结束行; i++)
                                    {
                                        sql = "update  [Bus_LablePrint] set 仓库 ='" + 结果值 + "'  where 生产批号='" + this.uiDataGridView产出.Rows[i].Cells["C生产批号"].Value.ToString() + "' ";
                                        int r = pub.ExecuteSql(pub.ConnectionString, sql, "");
                                        if (r > 0)
                                        {
                                            this.uiDataGridView产出.Rows[i].Cells["C仓库"].Value = 结果值;
                                        }
                                        else
                                        {
                                            MessageBox.Show("保存失败");
                                        }

                                    }
                                }
                            }
                        }

                        if (this.uiDataGridView产出.Columns[开始列].Name == "C表面状态" )
                        {
                            if (结果值 == "")
                            {

                            }
                            else
                            {    //检查未塞入用友
                                if (MessageBox.Show("您要批改所选择的表面状态(锌层)值么?", "", MessageBoxButtons.OKCancel) == DialogResult.OK)
                                {
                                    for (int i = 开始行; i <= 结束行; i++)
                                    {
                                        sql = "update  [Bus_LablePrint] set 锌层 ='" + 结果值 + "'  where 生产批号='" + this.uiDataGridView产出.Rows[i].Cells["C生产批号"].Value.ToString() + "' ";
                                        int r = pub.ExecuteSql(pub.ConnectionString, sql, "");
                                        if (r > 0)
                                        {
                                            this.uiDataGridView产出.Rows[i].Cells["C表面状态"].Value = 结果值;
                                        }
                                        else
                                        {
                                            MessageBox.Show("保存失败");
                                        }

                                    }
                                }
                            }
                        }

                        if (this.uiDataGridView产出.Columns[开始列].Name == "C表面处理")
                        {
                            if (结果值 == "")
                            {

                            }
                            else
                            {    //检查未塞入用友
                                if (MessageBox.Show("您要批改所选择的表面处理值么?", "", MessageBoxButtons.OKCancel) == DialogResult.OK)
                                {
                                    for (int i = 开始行; i <= 结束行; i++)
                                    {
                                        sql = "update  [Bus_LablePrint] set 表面处理 ='" + 结果值 + "'  where 生产批号='" + this.uiDataGridView产出.Rows[i].Cells["C生产批号"].Value.ToString() + "' ";
                                        int r = pub.ExecuteSql(pub.ConnectionString, sql, "");
                                        if (r > 0)
                                        {
                                            this.uiDataGridView产出.Rows[i].Cells["C表面处理"].Value = 结果值;
                                        }
                                        else
                                        {
                                            MessageBox.Show("保存失败");
                                        }

                                    }
                                }
                            }
                        }
                        
                        if (this.uiDataGridView产出.Columns[开始列].Name == "C产地")
                        {
                            if (结果值 == "")
                            {

                            }
                            else
                            {    //检查未塞入用友
                                if (MessageBox.Show("您要批改所选择的产地值么?", "", MessageBoxButtons.OKCancel) == DialogResult.OK)
                                {
                                    for (int i = 开始行; i <= 结束行; i++)
                                    {
                                        sql = "update  [Bus_LablePrint] set 产地 ='" + 结果值 + "'  where 生产批号='" + this.uiDataGridView产出.Rows[i].Cells["C生产批号"].Value.ToString() + "' ";
                                        int r = pub.ExecuteSql(pub.ConnectionString, sql, "");
                                        if (r > 0)
                                        {
                                            this.uiDataGridView产出.Rows[i].Cells["C产地"].Value = 结果值;
                                        }
                                        else
                                        {
                                            MessageBox.Show("保存失败");
                                        }

                                    }
                                }
                            }
                        }

                        if (this.uiDataGridView产出.Columns[开始列].Name == "C客户")
                        {
                            //检查未塞入用友
                            if (MessageBox.Show("您要批改所选择的客户值么?", "", MessageBoxButtons.OKCancel) == DialogResult.OK)
                            {
                                for (int i = 开始行; i <= 结束行; i++)
                                {
                                    sql = "update  [Bus_LablePrint] set 客户='" + 结果值 + "'  where 生产批号='" + this.uiDataGridView产出.Rows[i].Cells["C生产批号"].Value.ToString() + "' ";
                                    int r = pub.ExecuteSql(pub.ConnectionString, sql, "");
                                    if (r > 0)
                                    {
                                        this.uiDataGridView产出.Rows[i].Cells["C客户"].Value = 结果值;
                                    }
                                    else
                                    {
                                        MessageBox.Show("保存失败");
                                    }


                                }
                            }

                        }

                        if (this.uiDataGridView产出.Columns[开始列].Name == "C颜色")
                        {
                            //检查未塞入用友
                            if (MessageBox.Show("您要批改所选择的客户值么?", "", MessageBoxButtons.OKCancel) == DialogResult.OK)
                            {
                                for (int i = 开始行; i <= 结束行; i++)
                                {
                                    sql = "update  [Bus_LablePrint] set 颜色='" + 结果值 + "'  where 生产批号='" + this.uiDataGridView产出.Rows[i].Cells["C生产批号"].Value.ToString() + "' ";
                                    int r = pub.ExecuteSql(pub.ConnectionString, sql, "");
                                    if (r > 0)
                                    {
                                        this.uiDataGridView产出.Rows[i].Cells["C颜色"].Value = 结果值;
                                    }
                                    else
                                    {
                                        MessageBox.Show("保存失败");
                                    }


                                }
                            }

                        }

                    }

                }
            }

        }

        private void uiButton重新发送NC_Click(object sender, EventArgs e)
        {
            
            uiButton重新发送NC.Enabled = false;
            try
            {
                //遍历行
                if (uiDataGridView产出 != null && uiDataGridView产出.Rows.Count > 0)
                {
                    for (int i = 0; i < uiDataGridView产出.Rows.Count; i++)
                    {
                        if (this.uiDataGridView产出.Rows[i].Cells["C选择"].Value != null)//打钩
                        {
                            if ((bool)this.uiDataGridView产出.Rows[i].Cells["C选择"].Value)
                            {
                                //检查有没有存货编号
                                string 存货编号 = uiDataGridView产出.Rows[i].Cells["C存货编号"].Value.ToString();
                                if (string.IsNullOrEmpty(存货编号))
                                {
                                    MessageBox.Show("【" + uiDataGridView产出.Rows[i].Cells["C生产批号"].Value.ToString() + "】的存货编号为空，无法推送");
                                    continue;
                                }
                                string guid = uiDataGridView产出.Rows[i].Cells["Cguid"].Value.ToString();
                                string 生产批号 = uiDataGridView产出.Rows[i].Cells["C生产批号"].Value.ToString();
                                string 来源种类 = uiDataGridView产出.Rows[i].Cells["C来源种类"].Value.ToString();
                                string Customer = uiDataGridView产出.Rows[i].Cells["C客户"].Value.ToString();
                                string 品名 = uiDataGridView产出.Rows[i].Cells["C品名"].Value.ToString();
                                string 钢号 = uiDataGridView产出.Rows[i].Cells["C钢号"].Value.ToString();
                                string 规格 = uiDataGridView产出.Rows[i].Cells["C规格"].Value.ToString();
                                string 工厂编号 = uiDataGridView产出.Rows[i].Cells["C工厂编号"].Value.ToString();
                                string 来源产线 = uiDataGridView产出.Rows[i].Cells["C来源产线"].Value.ToString();

                                string 厚度 = uiDataGridView产出.Rows[i].Cells["C厚度"].Value.ToString();
                                string 长度 = uiDataGridView产出.Rows[i].Cells["C长度"].Value.ToString();
                                string 颜色 = uiDataGridView产出.Rows[i].Cells["C颜色"].Value.ToString();
                                //string 规格 = dt.Rows[e.RowIndex]["规格"].ToString();
                                string 等级 = uiDataGridView产出.Rows[i].Cells["C等级"].Value.ToString();
                                string 表面状态 = uiDataGridView产出.Rows[i].Cells["C表面状态"].Value.ToString();
                                string 表面处理 = uiDataGridView产出.Rows[i].Cells["C表面处理"].Value.ToString();
                                string 净重 = uiDataGridView产出.Rows[i].Cells["C净重"].Value.ToString();
                                string 备注 = uiDataGridView产出.Rows[i].Cells["C备注"].Value.ToString();
                                string 生产日期 = uiDataGridView产出.Rows[i].Cells["C生产日期"].Value.ToString();
                                string departCode = queryDeptCode(当前来源选项?.Tag?.ToString(), 生产批号);
                                string 仓库 = uiDataGridView产出.Rows[i].Cells["C仓库"].Value.ToString();
                                string 货位 = uiDataGridView产出.Rows[i].Cells["C货位"].Value.ToString();
                                string 锌层 = 表面状态;
                                string 包装形式 = uiDataGridView产出.Rows[i].Cells["C包装形式"].Value.ToString();
                                string sql标准 = "select QualityStandard from [dbo].[Dic_QualityStandard] where  WorkTypeName = '" + 当前来源选项.Text.ToString() + "'";
                                DataTable dt标准 = pub.GetDataTable(pub.ConnectionString, sql标准);
                                string 标准 = "";
                                if (dt标准 != null && dt标准.Rows.Count > 0)
                                {
                                    标准 = dt标准?.Rows[0][0].ToString();
                                }
                                
                                //产地暂时用万达
                                string 产地 = uiDataGridView产出.Rows[i].Cells["C产地"].Value.ToString();
                                string 抛NC成功 = uiDataGridView产出.Rows[i].Cells["C抛NC成功"].Value.ToString();

                                //先调用删除NC的接口
                                Dictionary<string, string> dic = new Dictionary<string, string>();
                                dic.Add("area", "华中");
                                dic.Add("method","delproduce");
                                dic.Add("pch", 生产批号);
                                string jsonStr = JsonConvert.SerializeObject(dic);
                                NCResponseModel nCResponseModel = HttpHelper.HttpPost<NCResponseModel>(NCDeleteProductUrl, jsonStr);
                                if (nCResponseModel != null && nCResponseModel.state != 0)
                                {
                                    string sqlUpdate = @"update Bus_LablePrint set 抛NC结果 = '" + nCResponseModel?.msg + "',是否抛送NC = 'N' , 抛NC时间 = '" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "' where 生产批号 = '" + 生产批号 + "'";
                                    int r = pub.ExecuteSql(pub.ConnectionString, sqlUpdate, "成品卷入库发送到NC");
                                    if (r < 1)
                                    {
                                        MessageBox.Show("3更新是否成功发送到NC失败");
                                    }
                                    else
                                    {
                                        uiDataGridView产出.Rows[i].Cells["C抛NC成功"].Value = "N";
                                        uiDataGridView产出.Rows[i].Cells["C抛NC结果"].Value = nCResponseModel.msg;
                                        this.uiDataGridView产出.Rows[i].Cells["C选择"].Value = false;
                                    }
                                    MessageBox.Show("["+ 生产批号 + "]无法重新推送,NC："+ nCResponseModel.msg);
                                    continue;
                                }
                                NCResponseModel nCResponse = sendToNC产成品(工厂编号, 仓库, 来源产线, guid, 存货编号, 生产批号, 净重, 生产日期, 品名,
                                                                  颜色, 长度, 产地, 锌层, 等级, 包装形式, Customer, 表面处理, 货位);

                                if (nCResponse != null && nCResponse.state == 0)
                                {
                                    string sqlUpdate = @"update Bus_LablePrint set 抛NC结果 = '" + nCResponse.result + "',是否抛送NC = 'Y' , 抛NC时间 = '" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "' where 生产批号 = '" + 生产批号 + "'";
                                    int r = pub.ExecuteSql(pub.ConnectionString, sqlUpdate, "成品卷入库发送到NC");
                                    //塞入入库单 stockVolume

                                    if (r < 1)
                                    {
                                        MessageBox.Show("1更新是否成功发送到NC失败");
                                    }

                                    else
                                    {
                                        uiDataGridView产出.Rows[i].Cells["C抛NC成功"].Value = "Y";
                                        uiDataGridView产出.Rows[i].Cells["C抛NC结果"].Value = nCResponse.result;
                                        this.uiDataGridView产出.Rows[i].Cells["C选择"].Value = false;
                                    }


                                    //塞入 Bus_Volume
                                    int res = insertIntoBus_Volume(工厂编号, 生产批号, 存货编号, departCode, 品名, 钢号, 长度, 厚度, 规格, 等级, 表面状态, 表面处理, 净重, 备注, 生产日期, 产地, 仓库, 锌层, 包装形式, 来源产线);
                                    //生成入库单 检查这个批号有没有生成过
                                    string docCode = deleteBus_StockInV(工厂编号, 生产批号, 存货编号, departCode, 品名, 钢号, 长度, 厚度, 规格, 等级, 表面状态, 表面处理, 净重, 备注, 生产日期, 产地, 锌层, 包装形式, Customer, 仓库, 来源产线, 颜色);
                               
                                }
                            else
                                {
                                    string sqlUpdate = @"update Bus_LablePrint set 抛NC结果 = '" + nCResponse?.msg + "',是否抛送NC = 'N' , 抛NC时间 = '" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "' where 生产批号 = '" + 生产批号 + "'";
                                    int r = pub.ExecuteSql(pub.ConnectionString, sqlUpdate, "成品卷入库发送到NC");
                                    if (r < 1)
                                    {
                                        MessageBox.Show("2更新是否成功发送到NC失败");
                                    }
                                    else
                                    {
                                        uiDataGridView产出.Rows[i].Cells["C抛NC成功"].Value = "N";
                                        uiDataGridView产出.Rows[i].Cells["C抛NC结果"].Value = nCResponse.msg;
                                        this.uiDataGridView产出.Rows[i].Cells["C选择"].Value = false;
                                    }
                                }
                            }

                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace);
            }
            finally
            {
                uiButton重新发送NC.Enabled = true;
            }
        }


        private void uiButton1_Click(object sender, EventArgs e)
        {
            if (uiDataGridView产出 != null && uiDataGridView产出.Rows != null && uiDataGridView产出.Rows.Count > 0)
            {
                for (int i = 0; i < uiDataGridView产出.Rows.Count; i++)
                {//选择了就取消 没选择就选择
                    this.uiDataGridView产出.Rows[i].Cells["C选择"].Value = true;
                }
            }
        }
        private void uiButton全不选_Click(object sender, EventArgs e)
        {
            if (uiDataGridView产出 != null && uiDataGridView产出.Rows != null && uiDataGridView产出.Rows.Count > 0)
            {
                for (int i = 0; i < uiDataGridView产出.Rows.Count; i++)
                {//选择了就取消 没选择就选择
                    this.uiDataGridView产出.Rows[i].Cells["C选择"].Value = false;
                }
            }
        }

        private void uiButton反选_Click(object sender, EventArgs e)
        {
            // 选择了就取消 没选择就选择
            if (uiDataGridView产出 != null && uiDataGridView产出.Rows != null && uiDataGridView产出.Rows.Count > 0)
            {
                for (int i = 0; i < uiDataGridView产出.Rows.Count; i++)
                {
                    if (this.uiDataGridView产出.Rows[i].Cells["C选择"].Value != null && (bool)this.uiDataGridView产出.Rows[i].Cells["C选择"].Value == true)//打钩
                    {
                        this.uiDataGridView产出.Rows[i].Cells["C选择"].Value = false;
                    }
                    else
                    if (this.uiDataGridView产出.Rows[i].Cells["C选择"].Value == null || (bool)this.uiDataGridView产出.Rows[i].Cells["C选择"].Value == false)
                    {
                        this.uiDataGridView产出.Rows[i].Cells["C选择"].Value = true;

                    }
                }
            }
        }

        private void uiButton导出模板_Click(object sender, EventArgs e)
        {
            string path;
            this.saveFileDialog1.Filter = "Excel文件 (*.XLSX)|*.XLSX";
            string sql = @"select 来源种类, 来源产线, 来源班组, 工厂编号, 生产批号, 存货编号, 品名, 钢号,卷径, 条码, 厚度,
                            产地, 长度, 规格, 等级, 表面状态, 表面处理, 净重, 备注, 生产日期, 检验员, 仓库, 货位,
                            锌层, 包装形式, 班组, 客户, 颜色,抗拉, 屈服, 延伸率 from Bus_LablePrint where 1 = 2";
            DataTable dt = pub.GetDataTable(pub.ConnectionString,sql);
            if (this.saveFileDialog1.ShowDialog(this) == DialogResult.OK)
            {
                path = this.saveFileDialog1.FileName;
                if (!File.Exists(path))
                {
                    MiniExcel.SaveAs(path, dt, true, "sheet1", ExcelType.XLSX);
                }
                else
                {
                    MessageBox.Show("文件已经存在,请重试!", "提示", MessageBoxButtons.OK);
                    return;
                }

            }
        }

        private void uiButton导入_Click(object sender, EventArgs e)
        {
            try
            {
                string path;
                openFileDialog1.RestoreDirectory = true;

                this.openFileDialog1.Filter = "Excel 文件(*.xlsx)|*.xlsx";
                if (this.openFileDialog1.ShowDialog(this) == DialogResult.OK)
                {
                    path = this.openFileDialog1.FileName;
                    DataTable table = (DataTable)MiniExcel.QueryAsDataTable(path, useHeaderRow: true);
                    //检查有没有导入过
                    if (table != null && table.Rows.Count > 0)
                    {
                        string[] 卷号Arrary = table.AsEnumerable().Select(row => "'" + row.Field<string>("生产批号") + "'").ToArray();
                        string para = string.Join(",", 卷号Arrary);
                        string sql = "select 生产批号 from Bus_LablePrint where 生产批号 in (" + para + ")";
                        DataTable dt = pub.GetDataTable(pub.ConnectionString, sql);
                        if (dt.Rows.Count > 0)
                        {
                            string 重复卷号 = "";
                            for (int i = 0; i < dt.Rows.Count; i++)
                            {
                                重复卷号 += dt.Rows[i]["生产批号"].ToString() + ",";
                            }
                            MessageBox.Show(重复卷号 + "卷号有重复，请检查");
                            return;
                        }
                        else
                        {
                            for (int i = 0; i < table.Rows.Count; i++)
                            {
                                string 来源种类 = table.Rows[i]["来源种类"].ToString();
                                string 来源产线 = table.Rows[i]["来源产线"].ToString();
                                string 来源班组 = table.Rows[i]["来源班组"].ToString();
                                string 工厂编号 = pub.全局工厂编号;
                                string 生产批号 = table.Rows[i]["生产批号"].ToString();
                                //string 存货编号 = table.Rows[i]["存货编号"].ToString();
                                string 存货编号 = "";
                                string 品名 = table.Rows[i]["品名"].ToString();
                                string 钢号 = table.Rows[i]["钢号"].ToString();
                                string 卷径 = table.Rows[i]["卷径"].ToString();
                                string 条码 = table.Rows[i]["生产批号"].ToString();
                                string 厚度 = table.Rows[i]["厚度"].ToString();
                                string 产地 = table.Rows[i]["产地"].ToString();
                                string 长度 = table.Rows[i]["长度"].ToString();
                                string 规格 = table.Rows[i]["规格"].ToString();
                                string 等级 = table.Rows[i]["等级"].ToString();
                                string 表面状态 = table.Rows[i]["表面状态"].ToString();
                                string 表面处理 = table.Rows[i]["表面处理"].ToString();
                                string 净重 = table.Rows[i]["净重"].ToString();
                                string 备注 = table.Rows[i]["备注"].ToString();
                                string 生产日期 = table.Rows[i]["生产日期"].ToString();
                                string 检验员 = table.Rows[i]["检验员"].ToString();
                                string 仓库 = table.Rows[i]["仓库"].ToString();
                                string 货位 = table.Rows[i]["货位"].ToString();
                                string 锌层 = table.Rows[i]["锌层"].ToString();
                                string 包装形式 = table.Rows[i]["包装形式"].ToString();
                                string 班组 = table.Rows[i]["班组"].ToString();
                                string 客户 = table.Rows[i]["客户"].ToString();
                                string 颜色 = table.Rows[i]["颜色"].ToString();
                                string 抗拉 = table.Rows[i]["抗拉"].ToString();
                                string 屈服 = table.Rows[i]["屈服"].ToString();
                                string 延伸率 = table.Rows[i]["延伸率"].ToString();
                                string sqlInsert = "insert into Bus_LablePrint ([guid], [来源种类], [来源产线], [来源班组], [工厂编号], [生产批号], [存货编号], [品名], [钢号],卷径, [条码], [厚度], [产地], [长度], [规格], [等级], [表面状态], [表面处理], [净重], [备注], [生产日期], [检验员], [操作ip], [操作人员], [操作时间], [仓库], [货位], [锌层], [包装形式], [班组], [客户], [颜色], [是否已打印], [打印次数], [是否抛送NC], [抛NC结果], [抛NC时间], [抗拉], [屈服], [延伸率])";
                                sqlInsert += "values( newid(), '"+ 来源种类 + "', '"+ 来源产线 + "', '"+ 来源班组 + "', '"+pub.全局工厂编号+"', '"+ 生产批号 + "', '"+ 存货编号 + "', '"+ 品名 + "', '"+ 钢号 + "','"+ 卷径 + "', '"+ 条码 + "', '"+ 厚度 + "', '"+ 产地 + "', '"+ 长度 + "', '"+ 规格 + "', '"+ 等级 + "', '"+ 表面状态 + "', '"+ 表面处理 + "', '"+ 净重 + "', '"+ 备注 + "', '"+ 生产日期 + "', '"+ 检验员 + "', '', '"+pub.sys_user.UserName+ "', getdate(), '"+ 仓库 + "', '"+ 货位 + "', '"+ 锌层 + "', '"+ 包装形式 + "', '"+ 班组 + "', '"+ 客户 + "', '"+ 颜色 + "', '否', '0', 'N', '', '', '"+ 抗拉 + "', '"+ 屈服 + "', '"+ 延伸率 + "')";
                                int R = pub.ExecuteSql(pub.ConnectionString, sqlInsert, "");
                            }
                        }
                    }
                }
                listData();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void uiButton导出_Click(object sender, EventArgs e)
        {
            string path;
            this.saveFileDialog1.Filter = "Excel文件 (*.XLSX)|*.XLSX";
            if (this.saveFileDialog1.ShowDialog(this) == DialogResult.OK)
            {
                path = this.saveFileDialog1.FileName;
                if (!File.Exists(path))
                {
                    DataTable dt = uiDataGridView产出.DataSource as DataTable;
                    MiniExcel.SaveAs(path, dt, true, "sheet1", ExcelType.XLSX);
                }
                else
                {
                    MessageBox.Show("文件已经存在,请重试!", "提示", MessageBoxButtons.OK);
                    return;
                }

            }
        }

        private void uiButton重新入库_Click(object sender, EventArgs e)
        {
            if (uiDataGridView产出 != null && uiDataGridView产出.Rows.Count > 0)
            {
                for (int i = 0; i < uiDataGridView产出.Rows.Count; i++)
                {
                    if (this.uiDataGridView产出.Rows[i].Cells["C选择"].Value != null)//打钩
                    {
                        if ((bool)this.uiDataGridView产出.Rows[i].Cells["C选择"].Value)
                        {
                            string 存货编号 = uiDataGridView产出.Rows[i].Cells["C存货编号"].Value.ToString();
                           
                            string guid = uiDataGridView产出.Rows[i].Cells["Cguid"].Value.ToString();
                            string 生产批号 = uiDataGridView产出.Rows[i].Cells["C生产批号"].Value.ToString();
                            string 来源种类 = uiDataGridView产出.Rows[i].Cells["C来源种类"].Value.ToString();
                            string Customer = uiDataGridView产出.Rows[i].Cells["C客户"].Value.ToString();
                            string 品名 = uiDataGridView产出.Rows[i].Cells["C品名"].Value.ToString();
                            string 钢号 = uiDataGridView产出.Rows[i].Cells["C钢号"].Value.ToString();
                            string 规格 = uiDataGridView产出.Rows[i].Cells["C规格"].Value.ToString();
                            string 工厂编号 = uiDataGridView产出.Rows[i].Cells["C工厂编号"].Value.ToString();
                            string 来源产线 = uiDataGridView产出.Rows[i].Cells["C来源产线"].Value.ToString();

                            string 厚度 = uiDataGridView产出.Rows[i].Cells["C厚度"].Value.ToString();
                            string 长度 = uiDataGridView产出.Rows[i].Cells["C长度"].Value.ToString();
                            string 颜色 = uiDataGridView产出.Rows[i].Cells["C颜色"].Value.ToString();
                            //string 规格 = dt.Rows[e.RowIndex]["规格"].ToString();
                            string 等级 = uiDataGridView产出.Rows[i].Cells["C等级"].Value.ToString();
                            string 表面状态 = uiDataGridView产出.Rows[i].Cells["C表面状态"].Value.ToString();
                            string 表面处理 = uiDataGridView产出.Rows[i].Cells["C表面处理"].Value.ToString();
                            string 净重 = uiDataGridView产出.Rows[i].Cells["C净重"].Value.ToString();
                            string 备注 = uiDataGridView产出.Rows[i].Cells["C备注"].Value.ToString();
                            string 生产日期 = uiDataGridView产出.Rows[i].Cells["C生产日期"].Value.ToString();
                            string departCode = queryDeptCode(当前来源选项?.Tag?.ToString(), 生产批号);
                            string 仓库 = uiDataGridView产出.Rows[i].Cells["C仓库"].Value.ToString();
                            string 货位 = uiDataGridView产出.Rows[i].Cells["C货位"].Value.ToString();
                            string 锌层 = 表面状态;
                            string 包装形式 = uiDataGridView产出.Rows[i].Cells["C包装形式"].Value.ToString();
                            string sql标准 = "select QualityStandard from [dbo].[Dic_QualityStandard] where  WorkTypeName = '" + 当前来源选项.Text.ToString() + "'";
                            DataTable dt标准 = pub.GetDataTable(pub.ConnectionString, sql标准);
                            string 标准 = "";
                            if (dt标准 != null && dt标准.Rows.Count > 0)
                            {
                                标准 = dt标准?.Rows[0][0].ToString();
                            }

                            //产地暂时用万达
                            string 产地 = uiDataGridView产出.Rows[i].Cells["C产地"].Value.ToString();
                            //检查有没有推NC了
                            string sqlLablePrint = $"select 抛NC结果,是否抛送NC from Bus_LablePrint where 生产批号 = '{生产批号}'";
                            DataTable dtLablePrint = pub.GetDataTable(pub.ConnectionString, sqlLablePrint);
                            if (dtLablePrint != null && dtLablePrint.Rows.Count > 0 && string.IsNullOrEmpty(dtLablePrint.Rows[0][0].ToString()))
                            {
                                MessageBox.Show($"{生产批号}还没有推送NC，不可以补入库。请通过批量打印或重新发送NC按钮操作");
                                continue;
                            }
                            //检查还在不在库
                            string sqlVolume = $"select IsInStorage from Bus_Volume where VolumeCodeNew = '{生产批号}'";
                            DataTable dt = pub.GetDataTable(pub.ConnectionString, sqlVolume);
                            if (dt != null && dt.Rows.Count > 0 && dt.Rows[0][0].ToString() == "出库")
                            {
                                MessageBox.Show($"{生产批号}已经出库，不可以补入库");
                                continue;
                            }

                            //塞入 Bus_Volume
                            int res = insertIntoBus_Volume(工厂编号, 生产批号, 存货编号, departCode, 品名, 钢号, 长度, 厚度, 规格, 等级, 表面状态, 表面处理, 净重, 备注, 生产日期, 产地, 仓库, 锌层, 包装形式, 来源产线);
                            //生成入库单 检查这个批号有没有生成过
                            string docCode = deleteBus_StockInV(工厂编号, 生产批号, 存货编号, departCode, 品名, 钢号, 长度, 厚度, 规格, 等级, 表面状态, 表面处理, 净重, 备注, 生产日期, 产地, 锌层, 包装形式, Customer, 仓库, 来源产线, 颜色);

                        }
                    }
                }
            }
            MessageBox.Show("补入库成功");
        }
    }

}
